tags:

views:

109

answers:

4

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  A: 

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) {
  ...
}
JaredPar
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. ;)
jalf
would you prefer ++it ?
Steve Townsend
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.
diverscuba23
i'm using MinGW and I can't use "auto"..
Michael Sync
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 Sync
@Michael, added a non-auto version
JaredPar
@jalf, but `auto` is so much more elegant :)
JaredPar
voted up since his gives both C++0x and pre C++0x answers
diverscuba23
@diverscuba23, yes it gives back a pair but I iterate agaist the `first` member.
JaredPar
If you're providing the non-`auto` version for C++03 compatibility reasons then you should also put a space between the two closing `>` of the type `pair<multimap<int, int>::const_iterator, multimap<int, int>::const_iterator> >` as without it is not valid in C++03.
Charles Bailey
@JaredPar: yep, I agree, auto looks much nicer. I'm just saying it might confuse someone who only knows C++03 (and may or may not know the *old* meaning of the keyword)
jalf
@JaredPar ok, comment was made before your edit.
diverscuba23
A: 

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++.

Charles Bailey
You mean, I don't need to use Find() ??
Michael Sync
@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)`.
Charles Bailey
+1  A: 

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);
hkaiser
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.
diverscuba23
pair<iterator, iterator> is giving me problem.
Michael Sync
What problems do you get? Is `myMultiMap` a const instance? If so, you need to use `typedef multimap<int, int>::const_iterator iterator;`.
hkaiser
+1  A: 

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;
}
diverscuba23
What are the differences between find and equal_range()?
Michael Sync
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.
diverscuba23