1

I've been googling for this for so long but I couldn't get the answer. The most of sample that I found are based on iterating with vector, map and etc..

I have the code below.

multimap<int, int>::iterator it = myMuliMap.find(1); 

Let's say I have three pairs that has key "1". I like to get those three pair from for loop.. I think that I can't use for(multimap::iterator anotherItr=myMuliMap.begin()..

The following code is in C#.. I like to get C++ version.. Thanks.

foreach(var mypair in it){
  Console.WriteLine(mypair.Key);
} 

4 Answers 4

4

The function you're looking for is equal_range. This returns an iterator to all pairs in the map which match the specified key

auto range = myMultiMap.equal_range(1);
for ( auto it = range.first; it != range.second; ++it) {
 ...
}

EDIT

Version without auto

pair<multimap<int,int>::const_iterator,multimap<int,int>::const_iterator>> it = myMultiMap.equal_range(1);
for ( multimap<int,int>::const_iterator it = range.first;
      it != range.second;
      ++it) {
  ...
}
Sign up to request clarification or add additional context in comments.

8 Comments

it might be a bit early to use auto without mentioning that it's not strictly speaking part of C++ yet. Give it another year or so. ;)
this will give an error as it will be std::pair<std::multimap<int,int>::iterator, std::multimap<int,int>::iterator> and not just a single iterator.
I already have multimap<int, int>::iterator it = myMuliMap.find(1); so I'm not so sure that I need equal_range(); Should I use equal_range instead of find()?
@Michael, added a non-auto version
@jalf, but auto is so much more elegant :)
|
1

Use std::equal_range():

int tolookfor = 1;
typedef multimap<int, int>::iterator iterator;
std::pair<iterator, iterator> p = 
    std::equal_range(myMuliMap.begin(), myMuliMap.end(), tolookfor);

for (iterator it = p.first; it != p.second ++it)
    std::cout << (*it).second << std::endl;

the multi_map's member function equal_range works similarily:

std::pair<iterator, iterator> p = 
    myMuliMap.equal_range(tolookfor);

3 Comments

if your looking for a value within the whole multimap, there is a version of equal_range that only takes the key as an argument.
pair<iterator, iterator> is giving me problem.
What problems do you get? Is myMultiMap a const instance? If so, you need to use typedef multimap<int, int>::const_iterator iterator;.
1

This will print out only the values found by

std::pair<std::multimap<int, int>::iterator, std::multimap<int, int>::iterator> result;
result = myMultimap.equal_range(1);

for(std::multimap<int,int>::iterator it = result.first; it != result.second; it++)
{
    std::cout << it->first << " = " << it->second << std:: endl;
}

2 Comments

What are the differences between find and equal_range()?
find will return an iterator to the first element that matches, or to a sentinal value that represents not found. equal_range will return a pair that represents the first element that is equal to the value you are looking for, and one past the last element that is equal to the value you are looking for.
0

You can use something like the following loop.

for (std::multimap<int, int>::iterator i  = myMultiMap.lower_bound(1);
                                       i != myMultiMap.upper_bound(1);
                                     ++i)
{
    std::cout << i->first << " => " << i->second << '\n';
}

This is valid in the current version of C++.

2 Comments

You mean, I don't need to use Find() ??
@Michael Sync: You don't need to, no. find and lower_bound do the same job, the only difference is what they return if there's no matching element. find(x) returns end() whereas lower_bound(x) points at first element in the sequence that sorts after the element that you were searching for, which is the same as upper_bound(x).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.