To create map of map in C++ is very simple.
Map is a sorted associative array of unique keys and associated data. The elements of the map are internally sorted from lowest to highest key value.
Example 1:
//Program to create multi level map in C++
#include <iostream>
#include <map> // header file needed for to use MAP STL
using namespace std;
int main(void)
{
//Define multilevel map. map within a map.
map<int, map<int,char> > myMap;
myMap[1][1] = 'A';
myMap[1][2] = 'B';
myMap[2][1] = 'C';
myMap[2][2] = 'D';
cout << "[1][1] = " << myMap[1][1] << endl;
cout << "[1][2] = " << myMap[1][2] << endl;
cout << "[2][1] = " << myMap[2][1] << endl;
cout << "[2][2] = " << myMap[2][2] << endl;
return 0;
}
//Save a file as multimap.cpp
To Compile and execute it on Ubuntu use following commands:
$ g++ multimap.cpp -o multimap
$ ./multimap
[1][1] = A
[1][2] = B
[2][1] = C
[2][2] = D
Example 2:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int, string> innerMap;
typedef map<double, innerMap> mainMap;
mainMap m;
void print()
{
map<double, innerMap >::iterator it;
map<int, string>::iterator inner_it;
for ( it=m.begin() ; it != m.end(); it++ ) {
cout << "\n\nNew element\n" << (*it).first << endl;
for( inner_it=(*it).second.begin(); inner_it != (*it).second.end(); inner_it++)
cout << (*inner_it).first << " => " << (*inner_it).second << endl;
}
}
int main (int argc, char *argv[])
{
// First insert an element into outermap
m.insert (make_pair (2.4, innerMap ()));
// Now insert elements into innermap
m[2.4].insert (make_pair (2, "two"));
m[2.4].insert (make_pair (5, "five"));
// First insert an element into outermap
m.insert (make_pair (0.6, innerMap ()));
// Now insert elements into innermap
m[0.6].insert (make_pair (5, "five"));
m[0.6].insert (make_pair (1, "one"));
m[0.6].insert (make_pair (2, "two"));
// print elements in the mainMap
print();
}
Example 3: Uses "multimap" STL
Note that, "mutimap" STL does not support [], therefore we cannot use syntax "m[].insert( make_pair())" we used in Example 2 to insert element.
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef multimap<int, string> innerMap;
multimap<double, innerMap> mainMap;
void print()
{
multimap<double, innerMap >::iterator it;
multimap<int, string>::iterator inner_it;
for ( it=mainMap.begin() ; it != mainMap.end(); it++ ) {
cout << "\n\nNew element\n" << (*it).first << endl;
for( inner_it=(*it).second.begin(); inner_it != (*it).second.end(); inner_it++)
cout << (*inner_it).first << " => " << (*inner_it).second << endl;
}
}
void insert1(multimap<double, innerMap >::iterator it)
{
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (2, "two"));
}
int main (int argc, char *argv[])
{
multimap<double, innerMap >::iterator it;
// First insert an element into outermap
it = mainMap.insert (make_pair (2.4, innerMap ()));
// Now insert elements into innermap
insert1(it);
// First insert an element into outermap
it = mainMap.insert (make_pair (0.6, innerMap ()));
// Now insert elements into innermap
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (1, "one"));
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (5, "five"));
// print elements in the mainMap
print();
}
Output of the above program (Example 3):
New element
0.6
1 => one
2 => two
5 => five
5 => five
5 => five
New element
2.4
2 => two
2 => two
5 => five
Example 4:
how to find out if a particular key exists in the map or not?
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef multimap<int, string> innerMap;
multimap<string, innerMap> mainMap;
void insert1(multimap<string, innerMap >::iterator it)
{
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (2, "two"));
}
int main (int argc, char *argv[])
{
multimap<string, innerMap >::iterator it;
// First insert an element into outermap
it = mainMap.insert (make_pair ("1st Element Key", innerMap ()));
// Now insert elements into innermap
insert1(it);
// First insert an element into outermap
it = mainMap.insert (make_pair ("2nd element Key", innerMap ()));
// Now insert elements into innermap
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (1, "one"));
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (5, "five"));
// Find the key in the mainMap
it = mainMap.find("2nd element Key");
if (it != mainMap.end()) {
cout << "\n Key Found!" << endl;
} else {
cout << "\n Key Not Found!" << endl;
}
}
Output of the above program (Example 4):
Key Found!
Map is a sorted associative array of unique keys and associated data. The elements of the map are internally sorted from lowest to highest key value.
Example 1:
//Program to create multi level map in C++
#include <iostream>
#include <map> // header file needed for to use MAP STL
using namespace std;
int main(void)
{
//Define multilevel map. map within a map.
map<int, map<int,char> > myMap;
myMap[1][1] = 'A';
myMap[1][2] = 'B';
myMap[2][1] = 'C';
myMap[2][2] = 'D';
cout << "[1][1] = " << myMap[1][1] << endl;
cout << "[1][2] = " << myMap[1][2] << endl;
cout << "[2][1] = " << myMap[2][1] << endl;
cout << "[2][2] = " << myMap[2][2] << endl;
return 0;
}
//Save a file as multimap.cpp
To Compile and execute it on Ubuntu use following commands:
$ g++ multimap.cpp -o multimap
$ ./multimap
[1][1] = A
[1][2] = B
[2][1] = C
[2][2] = D
Example 2:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int, string> innerMap;
typedef map<double, innerMap> mainMap;
mainMap m;
void print()
{
map<double, innerMap >::iterator it;
map<int, string>::iterator inner_it;
for ( it=m.begin() ; it != m.end(); it++ ) {
cout << "\n\nNew element\n" << (*it).first << endl;
for( inner_it=(*it).second.begin(); inner_it != (*it).second.end(); inner_it++)
cout << (*inner_it).first << " => " << (*inner_it).second << endl;
}
}
int main (int argc, char *argv[])
{
// First insert an element into outermap
m.insert (make_pair (2.4, innerMap ()));
// Now insert elements into innermap
m[2.4].insert (make_pair (2, "two"));
m[2.4].insert (make_pair (5, "five"));
// First insert an element into outermap
m.insert (make_pair (0.6, innerMap ()));
// Now insert elements into innermap
m[0.6].insert (make_pair (5, "five"));
m[0.6].insert (make_pair (1, "one"));
m[0.6].insert (make_pair (2, "two"));
// print elements in the mainMap
print();
}
Output of above program (Example 2):
New element
0.6
1 => one
2 => two
5 => five
New element
2.4
2 => two
5 => five
Example 3: Uses "multimap" STL
Note that, "mutimap" STL does not support [], therefore we cannot use syntax "m[].insert( make_pair())" we used in Example 2 to insert element.
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef multimap<int, string> innerMap;
multimap<double, innerMap> mainMap;
void print()
{
multimap<double, innerMap >::iterator it;
multimap<int, string>::iterator inner_it;
for ( it=mainMap.begin() ; it != mainMap.end(); it++ ) {
cout << "\n\nNew element\n" << (*it).first << endl;
for( inner_it=(*it).second.begin(); inner_it != (*it).second.end(); inner_it++)
cout << (*inner_it).first << " => " << (*inner_it).second << endl;
}
}
void insert1(multimap<double, innerMap >::iterator it)
{
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (2, "two"));
}
int main (int argc, char *argv[])
{
multimap<double, innerMap >::iterator it;
// First insert an element into outermap
it = mainMap.insert (make_pair (2.4, innerMap ()));
// Now insert elements into innermap
insert1(it);
// First insert an element into outermap
it = mainMap.insert (make_pair (0.6, innerMap ()));
// Now insert elements into innermap
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (1, "one"));
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (5, "five"));
// print elements in the mainMap
print();
}
Output of the above program (Example 3):
New element
0.6
1 => one
2 => two
5 => five
5 => five
5 => five
New element
2.4
2 => two
2 => two
5 => five
Example 4:
how to find out if a particular key exists in the map or not?
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef multimap<int, string> innerMap;
multimap<string, innerMap> mainMap;
void insert1(multimap<string, innerMap >::iterator it)
{
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (2, "two"));
}
int main (int argc, char *argv[])
{
multimap<string, innerMap >::iterator it;
// First insert an element into outermap
it = mainMap.insert (make_pair ("1st Element Key", innerMap ()));
// Now insert elements into innermap
insert1(it);
// First insert an element into outermap
it = mainMap.insert (make_pair ("2nd element Key", innerMap ()));
// Now insert elements into innermap
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (1, "one"));
it->second.insert (make_pair (2, "two"));
it->second.insert (make_pair (5, "five"));
it->second.insert (make_pair (5, "five"));
// Find the key in the mainMap
it = mainMap.find("2nd element Key");
if (it != mainMap.end()) {
cout << "\n Key Found!" << endl;
} else {
cout << "\n Key Not Found!" << endl;
}
}
Output of the above program (Example 4):
Key Found!
Good post.
ReplyDeleteIn example 3, if u have both the key and value as strings, how to find out if a particular key exists in the map or not?
Pls give a solution
The "map" and "multimap" contains "find" function to search for an element in it. I added the solution as Example 4.
DeleteThanks. It was simple and helpful.
ReplyDeleteThank you so much :) Like it said before simple and helpful
ReplyDeleteI finally can add the values to my map but now when I want to send this values to an archive I get an error of Iterator not dereferencable. Any idea of how can I solve this :x
ReplyDeleteCould you please provide snippet of the code?
DeleteIt was my mistake I wasnt using properly the inner it, your code works perfectly
DeleteThank you