[Solved] How to find all elements in vector that are greater than certain number? [duplicate]


You would use a loop and access the amount member:

#include <iostream>
#include <vector>
#include <string>

struct Record
{
  int amount;
  std::string name;
};

static const Record database[] =
{
  {  5, "Padme"},
  {100, "Luke"},
  { 15, "Han"},
  { 50, "Anakin"},
};
const size_t database_size =
    sizeof(database) / sizeof(database[0]);

int main()
{
    std::vector<Record> vector1;

    // Load the vector from the test data.
    for (size_t index = 0; index < database_size; ++index)
    {
        vector1.push_back(database[index]);
    }

    const int key_amount = 20;
    const size_t quantity = vector1.size();
    for (size_t i = 0U; i < quantity; ++i)
    {
      const int amount = vector1[i].amount;
      if (amount > key_amount)
      {
        std::cout << "Found at [" << i << "]: "
                  << amount << ", "
                  << vector1[i].name
                  << "\n";
      }
    }
    return 0;
}

Here’s the output:

$ ./main.exe
Found at [1]: 100, Luke
Found at [3]: 50, Anakin

10

solved How to find all elements in vector that are greater than certain number? [duplicate]