I have a vector
of struct
s which have about 100 members within the struct
. The vector
itself can grow to be as large as 1000 elements. I am trying to find a simple way to search the list based on a set of 3 elements every struct
contains amongst its many members:
- std::string firstName;
- std::string lastName;
- size_t age;
I'm trying to find a way to search the vector
based on a key derived from these three values, rather than iterating through the list and doing something like:
for ( int i = 0; i < list.length(); i++ )
{
if (element[i].lastName == lastNameToFind &&
element[i].firstName == firstNameToFind &&
element[i].age == ageToFind)
{
// found the element
}
}
I am looking for faster methods that take advantage of the underlying logic in std::vector
to operate more efficiently, and if I want to search by different key tuples, I just change a couple lines of code rather than writing another search function. Is such an approach possible?