Apr 9, 2009 at 1:39am UTC
Hi, I am trying to use multimap of multimap, because i would like to have multiple keys like multimap[key1][key2] = value. this is happening if i use map. But when i use multimap of multimap, i have to use equal_range() to find the key in the first multimap then i am getting all the values from the second multimap using iterator. But it is getting slow for finding a value in second multimap for huge data collection. Please help me in finding in both the multiple maps faster.
Thanks for your help.
my sample code(am using GCC compiler in HPUX)
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef multimap<int, int> t_mmap_inner;
typedef t_mmap_inner::iterator it_minner;
typedef pair<t_mmap_inner::iterator,t_mmap_inner::iterator> mit_inner;
typedef multimap<string, t_mmap_inner > t_mmap_outer;
typedef t_mmap_outer::iterator it_mouter;
typedef pair<t_mmap_outer::iterator,t_mmap_outer::iterator> mit_outer;
int main() {
t_mmap_outer mmm;
t_mmap_inner mm;
mm.insert(pair<int,int>(6,5));
mm.insert(pair<int,int>(7,6));
mm.insert(pair<int,int>(7,5));
mm.insert(pair<int,int>(3,5));
cout << "inner map contains" << endl;
for (it_minner j = mm.begin(); j != mm.end(); ++j)
cout << " " << j->first << ", " << j->second << '\n';
mmm.insert(pair<string, t_mmap_inner>("ABC",mm));
mm.clear();
mm.insert(pair<int,int>(10,5));
mm.insert(pair<int,int>(7,10));
mmm.insert(pair<string, t_mmap_inner>("EFG",mm));
mm.clear();
mm.insert(pair<int,int>(7,15));
mm.insert(pair<int,int>(13,7));
mmm.insert(pair<string, t_mmap_inner>("JKL",mm));
mmm.insert(pair<string, t_mmap_inner>("ABC",mm));
//printing all the values in the outer map
for (it_mouter i = mmm.begin(); i != mmm.end(); ++i) {
cout << i->first << '\n';
for (it_minner j = i->second.begin(); j != i->second.end(); ++j)
cout << " " << j->first << ", " << j->second << '\n';
}
mit_inner mit;
it_minner itm;
it_minner itmi;
mit_outer mito;
it_mouter itmo;
//finding all the items with ABC and 7
mito = mmm.equal_range("ABC");
for (itmo=mito.first; itmo!=mito.second; ++itmo){
cout << " " << (*itmo).first << endl;
for (itm = itmo->second.begin(); itm != itmo->second.end(); ++itm){
if (itm->first==7)
cout << itm->second << endl;
}
}
mmm.clear();
return 0;
}
Apr 9, 2009 at 3:01am UTC
maybe you could make map of paired keys being the key. something like
1 2 3 4 5 6 7 8 9 10 11 12
struct pairdKeys {
int key1;
int key2;
paired keys(int k1, int k2) : key1(k1), key2(k2) {}
bool operator < (pairdKeys& right) {
if (key1 < right.key1)
return true ;
if (right.key1 < key1)
return false ;
return key2 < right.key2;
}
}
not sure how it would compare in efficiency, and it's kind of messy to use.
Last edited on Apr 10, 2009 at 3:07am UTC