A map is a datastructure to store a key value pair.
#include <map>
-
It is a datastructure which stores a key value pair.
-
Elements are referenced using their keys not by position(unlike arrays).
-
The elements are stored in orders(i.e. they are pre-sorted).
-
Internal implementation is like a binary search tree.
-
Size is increased and decreased automatically, according to storage needs.
-
Keys are unique. Same key cannot be used to store more than 1 values.
Template has two data types first for key and second for value. User defined comparator can be added as third parameter.
Makes an empty container.```cpp map var_name; //Starts the var_name map with no key value pairs ```
Used to copy range of elements from one map to other.
map<Type1, Type2> var_name1;
//fill 10 elements in var_name
//If want to copy elements from 0-10
map<Type1, Type2> var_name2(var_name1.begin(),var_name1.end());
Note: The template types should be same.
map<Type1, Type2> var_name(map_object);
Copies the map_object in var_name. The map_object should be of same type.
map<char, int> map_object;
map_object['a'] = 10;
map_object.insert(make_pair<char, int>('b', 20));
The return_object is a pair, first is an iterator to inserted element(or the same key element if insertion was prevented) and second is a boolean object which is true if insert happened and is false if it was stopped.
#include<maps>
map<char,int> mymap;
mymap.insert ( pair<char,int>('a',100) );
mymap.insert ( pair<char,int>('z',200) );
pair<std::map<char,int>::iterator,bool> ret;
ret = mymap.insert (pair<char,int>('z',500) );
if (ret.second==false) {
cout << "element 'z' already existed";
cout << " with a value of " << ret.first->second << '\n';
}
cout << mymap['b'];
cout << mymap.at('b');
cout << mymap.find('b')->second;
cout << (*mymap.find('b')).second;
mymap.erase('b');
it = mymap.find('a');
mymap.erase(it, mymap.end());
map<char,int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); it++)
std::cout << it->first << " => " << it->second << '\n';
return 0;
An unordered map is a datastructure to store a key value pair and access it fast enough.
#include <unordered_map>
-
It is a datastructure which stores a key value pair.
-
Elements are referenced using their keys not by position(unlike arrays).
-
The elements are not sorted.
-
Internal implementation is using hashing.
-
Size is increased and decreased automatically, according to storage needs.
-
Keys are unique. Same key cannot be used to store more than 1 values.
-
Introduced in C++11.
Template has two data types first for key and second for value.
Makes an empty container.unordered_map<Type1, Type2> var_name; //Starts the var_name map with no key value pairs
Used to copy range of elements from one map to other.
unordered_map<Type1, Type2> var_name1;
//fill 10 elements in var_name
//If want to copy elements from 0-10
unordered_map<Type1, Type2> var_name2(var_name1.begin(),var_name1.end());
Note: The template types should be same.
unordered_map<Type1, Type2> var_name(map_object);
Copies the map_object in var_name. The map_object should be of same type.
unordered_map<char, int> map_object;
map_object['a'] = 10;
map_object.insert(make_pair<char, int>('b', 20));
The return_object is a pair, first is an iterator to inserted element(or the same key element if insertion was prevented) and second is a boolean object which is true if insert happened and is false if it was stopped.
unordered_map<char,int> mymap;
mymap.insert ( pair<char,int>('a',100) );
mymap.insert ( pair<char,int>('z',200) );
pair<std::map<char,int>::iterator,bool> ret;
ret = mymap.insert (pair<char,int>('z',500) );
if (ret.second==false) {
cout << "element 'z' already existed";
cout << " with a value of " << ret.first->second << '\n';
}
cout << mymap['b'];
cout << mymap.at('b');
cout << mymap.find('b')->second;
cout << (*mymap.find('b')).second;
mymap.erase('b');
it = mymap.find('a');
mymap.erase(it, mymap.end());
unordered_map<char,int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
for (unordered_map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); it++)
cout << it->first << " => " << it->second << '\n';
return 0;
A Multimap is a datastructure to store a key value pair having multiple keys.
#include <map>
Same as maps but only it can have multiple values for a key.
Template has two data types first for key and second for value. User defined comparator can be added as third parameter.
Makes an empty container.multimap<Type1, Type2> var_name; //Starts the var_name map with no key value pairs
Used to copy range of elements from one map to other.
multimap<Type1, Type2> var_name1;
//fill 10 elements in var_name
//If want to copy elements from 0-10
multimap<Type1, Type2> var_name2(var_name1.begin(),var_name1.end());
Note: The template types should be same.
multimap<Type1, Type2> var_name(map_object);
Copies the map_object in var_name. The map_object should be of same type.
Example Returns a pair of iterators. First is an iterator to the first element having the given key. Second is the iterator to the next to last element having the key.
Example Deletes an element using its iterator. Same as maps and unordered_maps.
Example Same as maps and unordered_maps.
Example