Multimap?
-
Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?
-
Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?
multimap<string, string> mymap;
string strKey;:
:multimap<string, string>::iterator it;
:
it = mymap.find( strKey );
if(it != mymap.end())
{
do
{
cout << strKey << " = " << it->second << "\n";
it++;
}
while( it != mymap.upper_bound( strKey ));
} -
Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?
Try something like this:
// MultiMap.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>void Go()
{
using namespace std;typedef multimap<int, string> Tmm; typedef Tmm::value\_type TmmVal; typedef Tmm::const\_iterator TmmIt; Tmm mm; mm.insert(TmmVal(1, "One")); mm.insert(TmmVal(1, "Single")); mm.insert(TmmVal(2, "Two")); mm.insert(TmmVal(2, "1+1")); mm.insert(TmmVal(2, "Duo")); mm.insert(TmmVal(3, "Three")); pair<TmmIt, TmmIt> ip = mm.equal\_range(2); for (TmmIt it=ip.first; it!=ip.second; ++it) { cout << it->first << " : " << it->second << endl; }
}
int main(int argc, char* argv[])
{
Go();return 0;
}
Output is:
2 : Two
2 : 1+1
2 : DuoSteve
-
multimap<string, string> mymap;
string strKey;:
:multimap<string, string>::iterator it;
:
it = mymap.find( strKey );
if(it != mymap.end())
{
do
{
cout << strKey << " = " << it->second << "\n";
it++;
}
while( it != mymap.upper_bound( strKey ));
}This is inefficient. Why calculate the upper bound each loop?
Steve
-
I assume any standard compliant one will do.
Steve
-
I assume any standard compliant one will do.
Steve