I have an STL :: multimap and I am looking for it with equal_range to return the upper and lower bounds. Can I find the number of elements in this range without repeating all of them and counting them one at a time?
#include <iostream>
#include <map>
using namespace std;
int main () {
multimap<int,int> mm;
pair<multimap<int, int>::iterator,multimap<int, int>::iterator> ret;
multimap<int,int>::iterator retit;
for (int n=0; n<100; n++) {
mm.insert ( make_pair( rand()%10,rand()%1000) );
}
ret = mm.equal_range(5);
int ct = 0;
for (retit=ret.first; retit!=ret.second; ++retit) {
cout << retit->second << endl;
ct++;
}
cout << ct << endl;
return 0;
}
source
share