Hi,

I have a written a program using multimap and its not giving me the results I am expecting. I have used pair in my program. There is some problem which I am unable to figure out. Perhaps I am using pair for the first time with multimap.

Here is my code:-

#include <iostream>
#include <map>
#include <set>
using namespace std;

int main ()
{
  multimap<string,int> mm;
  multimap<string,int>::iterator it;
  multimap<string,multimap<string,int> > mymm;
  multimap<string,multimap<string,int> >::iterator itx;

  pair<multimap<string,pair<string,int> >::iterator,multimap<string,pair<string,int> >::iterator> ii;


  mm.insert(pair<string,int>("b",20));
  mm.insert(pair<string,int>("b",30));
  mm.insert(pair<string,int>("b",40));
  mm.insert(pair<string,int>("c",50));
  mm.insert(pair<string,int>("c",60));
  mm.insert(pair<string,int>("d",60));

  mymm.insert(make_pair("1", mm));
  mymm.insert(make_pair("1", mm));
  mymm.insert(make_pair("2", mm));
  mymm.insert(make_pair("2", mm));
  mymm.insert(make_pair("3", mm));
  mymm.insert(make_pair("3", mm));


for (itx = mymm.begin(); itx != mymm.end(); ++itx)
{
  cout << "  [" << itx->first << "] " << itx->second.begin()->first << " " <<  itx->second.begin()->second << endl;
}

  return 0;
}

Here are the results:-

[1] b 20
  [1] b 20
  [2] b 20
  [2] b 20
  [3] b 20
  [3] b 20

Its printing only the first elements

[1] b 20

I expect the results like this:-

[1] b 20
  [1] b 30
  [2] b 40
  [2] b 50
  [3] b 60
  [3] b 60

I would actually like to filter my results like suppose I want only keys with "2" to be printed:-

[2] b 40
  [2] b 50

Thanks

> Its printing only the first elements

Well, you have a multimap which contains multimaps :
multimap<string,multimap<string,int> > mymm;

So you need a nested loop to print the contents of the inner multimaps. Something like:

for (itx = mymm.begin(); itx != mymm.end(); ++itx)
{
  cout << "  [" << itx->first << "] " ;
  for( it = itx->second.begin() ; it != itx->second.end() ; ++it )
        cout << it->first << ',' <<  it->second << "   " ;
  cout << '\n' ;
}

Edited 6 Years Ago by vijayan121: n/a

> Its printing only the first elements

Well, you have a multimap which contains multimaps :
multimap<string,multimap<string,int> > mymm;

So you need a nested loop to print the contents of the inner multimaps. Something like:

for (itx = mymm.begin(); itx != mymm.end(); ++itx)
{
  cout << "  [" << itx->first << "] " ;
  for( it = itx->second.begin() ; it != itx->second.end() ; ++it )
        cout << it->first << ',' <<  it->second << "   " ;
  cout << '\n' ;
}

Hi.. I made some changes like this:-

for (itx = mymm.begin(); itx != mymm.end(); ++itx){

    if(itx->first.compare("1")==0){
             cout << " [" << itx->first << "] " ;
       for( it = itx->second.begin() ; it != itx->second.end() ; ++it )
             cout << it->first << ',' << it->second << " " ;
             cout << '\n' ;
       }
     }

I am getting results like below:-

[1] b,20 b,30 b,40 c,50 c,60 d,60 
 [1] b,20 b,30 b,40 c,50 c,60 d,60

But I want the results to come like this:-

[1] b,20 
 [1] b,30

How can I do that ?
Thanks

Hi ..

I tried another way like this:-

#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;

int main ()
{
  multimap<string,int> mm;
  multimap<string,int>::iterator it;
  multimap<vector<string>,multimap<string,int> > mymm;
  multimap<vector<string>,multimap<string,int> >::iterator itx;
 
  vector<string> v1;
  vector<string>::iterator itv;

  

  pair<multimap<vector<string>,pair<string,int> >::iterator,multimap<vector<string>,pair<string,int> >::iterator> ii;


  mm.insert(pair<string,int>("a",10));
  mm.insert(pair<string,int>("b",20));
  mm.insert(pair<string,int>("c",30));
  mm.insert(pair<string,int>("d",40));
  mm.insert(pair<string,int>("e",50));
  mm.insert(pair<string,int>("f",60));

  v1.push_back("1");
  v1.push_back("2");
  v1.push_back("3");
  v1.push_back("4");
  v1.push_back("5");
  v1.push_back("6");  

  mymm.insert(make_pair(v1,mm));


  for (itx = mymm.begin(); itx != mymm.end(); ++itx){

        for ( itv= v1.begin() ; itv < v1.end(); itv++ ){
             if(*itv=="1"){
                  for( it = itx->second.begin() ; it != itx->second.end() ; ++it ){
                          cout << it->first << ',' << it->second << " " ;
                          cout << '\n' ;
                  }
             }
        }
  }

  return 0;
}

The output is :-

a,10 
b,20 
c,30 
d,40 
e,50 
f,60

I expected only

a,10

since I am trying to filter using if loop like this

if(*itv=="1")

How can I get the desired results of only

a,10

Thanks

This article has been dead for over six months. Start a new discussion instead.