I am searching for all pairs for a particular key in a multimap using the code below.
int main() {
multimap<int,int> mp;
mp.insert({1,2});
mp.insert({11,22});
mp.insert({12,42});
mp.insert({1,2});
mp.insert({1,2});
for (auto itr = mp.find(1); itr != mp.end(); itr++)
cout << itr->first<< '\t' << itr->second << '\n';
}
find
returns an iterator into the map where the element is found. It doesn't create something new with just the matched results. – Deciliter1
key - which it does - and then iterate to the end of the container - which it does. I suspect that you may be confused about what an iterator is (itr
), or what it means to increment an iterator (itr++
), or whatmp.end()
signifies. – Glean