I am using an object as a key for a multimap. The object is a custom date class which I created. I was just wondering if it is possible to use a variable found in the object for equal_range()?
That is check against the month variable in my custom date object.
That is something like this (pseudo code).
int january = 1;
foundValues = myMultimap.equal_range(january);
for (it=foundValues.first; it!=foundValues.second; ++it)
{
cout << " " << (*it).second;
cout << endl;
}
Will this go through each key object and check if the variable inside that object is equal to “january”, then return the value paired to the key?
Thank you.
The search members of the associative container work on the key only. Theyexist because they take advantage of the container’s internal structure which guarantees finding values (or their absence) in
O(log(n))time (wherenis the number of elements in the container).If you want to find all eleements in a sequence matching a condition you probably want to use
std::copy_if()with a suitable predicate.