Multimap in multimap with C++ — an example

multimap example in c++

A colleague of mine is working in NXOpen and extracting the boundary conditions of the finite element model using Multimap.

Looking at the code, I suggested that he would be better up if he choose to use Multimap in Multimap for the functionality he is after.

To demonstrate the multimap in multimap concept, wrote a simple multimap in multimap example in c++

It helped him and should simplify his work. Putting it here in the hope that this will help someone.



#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
 
 
using namespace std;
 
void printmapvalues(multimap<string,int> p1,string ss1);
void printallmap(multimap<string,int> p1);
void printallmap2(multimap<string,multimap<string,int>> me);
 
int main()
{
      multimap<string,int> mmap,mmap2;  // simple string integer multimap
    multimap<string,multimap<string,int>> mera; // multimap of multimap
   
      // add some values
      mmap.insert(pair<string,int>("SSS",11));
      mmap.insert(pair<string,int>("SSS",11));
      mmap.insert(pair<string,int>("SSS",21));
      mmap.insert(pair<string,int>("SSS",13));
      mmap.insert(pair<string,int>("SSS",31));
      mmap.insert(pair<string,int>("MMM",31));
      mmap.insert(pair<string,int>("MMM",41));
      mmap.insert(pair<string,int>("MMM",51));
      mmap.insert(pair<string,int>("MMM",61));
      mmap.insert(pair<string,int>("MMM",11));
 
// add another multimpap valuess
      mmap2.insert(pair<string,int>("Sim",301));
      mmap2.insert(pair<string,int>("Mim",310));
 
// insert the two above multimaps to this multimap in multimap
      mera.insert(pair<string,multimap<string,int>>("Office",mmap));
    mera.insert(pair<string,multimap<string,int>>("Ghar",mmap2));
 
//    cout<<"Size of  ss "<<mmap.count("SSS")<<endl; // cont the number of value for SSS
    printallmap(mmap);
      printallmap(mmap2);
      printmapvalues(mmap,"MMM");
      printallmap2(mera);
     
      return 0;
}
 
void printmapvalues(multimap<string,int> p1,string ss1){
// print values of a given filed of a malutimap
      pair <multimap<string,int>::iterator, multimap<string,int>::iterator> ret;
      ret = p1.equal_range(ss1);
 
      for (multimap<string,int>::iterator it=ret.first; it!=ret.second; ++it){
        cout << ' ' << it->second;
      cout << '\n';
      }
 
}
 
void printallmap(multimap<string,int> p1){
// print all values of a multimap
      for (multimap<string,int>::iterator it=p1.begin(); it!=p1.end(); ++it){
      cout << it->first << ' ' << it->second << '\n';
      }
}
 
void printallmap2(multimap<string,multimap<string,int>> me){
// prints values of a multimap containing a multimap
      for (multimap<string,multimap<string,int>>::iterator it=me.begin(); it!=me.end(); ++it){
     cout<<it->first<<" contains...."<<endl    ;
            printallmap(it->second);
      }
 
}

One thought on “Multimap in multimap with C++ — an example

  1. Pingback: Mixing debug and release libraries in Visual Studio | SukhbinderSingh.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s