map in c++
xxxxxxxxxx
std::map<key_type, value_type> map_name = {{key1, value1},{key2, value2}, };
if {1,1,1,2,2,3,3,3,3,4} then after operation for key1 = 4 value will be 1
xxxxxxxxxx
//assuming your variables are called : variables_
#include <map>
#include <string>
std::map<int, std::string> map_;
map_[1] = "mercury";
map_[2] = "mars";
map_.insert(std::make_pair(3, "earth"));
//either synthax works to declare a new entry
return map_[2]; //here, map_[2] will return "mars"
xxxxxxxxxx
#include <map>
// empty map container
map<int, int> gquiz1;
// insert elements in random order
gquiz1.insert(pair<int, int>(1, 40));
xxxxxxxxxx
#include <map>
int main(){
//new map
std::map <int,int> myMap;
myMap[0] = 1;
myMap[1] = 2;
myMap[2] = 3;
myMap[3] = 4;
}
xxxxxxxxxx
#include <map>
// ===== CONSTRUCTOR
// map (char -> int)
std::map<char,int> map_int;
map_int['a']=10;
map_int['b']=30;
// map (int -> string)
std::map<int, std::string> map_string;
map_string[24] = "46";
// ===== EXISTENCE and SIZE
// map size
map_int.size() : size_type (int)
// search for 'a'
// map.find(key) : map.iterator
if(map_int.find('a') != map_int.end()) {/*...*/}
// map_iterator->first : type(key)
// map_iterator->second : type(value)
// check if the map is empty
map_int.empty() // : bool
// ===== SET
// just name and set
// if the element already exists, the statement will override that value
map_int['a']=10;
// check before set
if(map_int.find('a') == map_int.end())
map_int['a']=10;
// ===== DELETE
// first: iterator, then: erase(it)
auto it = map_int.find('a');
if(it != map_int.end())
map_int.erase(it);
// or also, using keys
map_int.erase('a');
xxxxxxxxxx
#include <iostream>
#include <map>
using namespace std;
int main(){
map <char,int> mp;
map <char,int> mymap,mymap1;
//insert elements individually in map with the combination of key value and value of element
mp.insert(pair<char,int>('a',2)); //key is 'c' and 2 is value.
mp.insert(pair<char,int>('b',1));
mp.insert(pair<char,int>('c',43));
//inserts elements in range using insert() function in map 'mymap'.
mymap.insert(mp.begin(),mp.end());
//declaring iterator for map
map <char,int>::iterator it;
//using find() function to return reference of element mapped by key 'b'.
it = mp.find('b');
//prints key and element's value.
cout<<"Key and element's value of map are: ";
cout<<it->first<<" and "<<it->second<<endl;
//alternative way to insert elements by mapping with their keys.
mymap1['x'] = 23;
mymap1['y'] = 21;
cout<<"Printing element mapped by key 'b' using at() function : "<<mp.at('b')<<endl;
//swap contents of 2 maps namely mymap and mymap1.
mymap.swap(mymap1);
/* prints swapped elements of mymap and mymap1 by iterating all the elements through
using iterator. */
cout<<"Swapped elements and their keys of mymap are: "<<endl;
for(it=mymap.begin();it!=mymap.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<"Swapped elements and their keys of mymap1 are: "<<endl;
for(it=mymap1.begin();it!=mymap1.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
//erases element mapped at 'c'.
mymap1.erase('c');
//prints all elements of mymap after erasing element at 'c'.
cout<<"Elements of mymap1 after erasing element at key 'c' : "<<endl;
for(it=mymap1.begin();it!=mymap1.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
//erases elements in range from mymap1
mymap1.erase(mymap1.begin(),mymap1.end());
cout<<"As mymap1 is empty so empty() function will return 1 : " << mymap1.empty()<<endl;
//number of elements with key = 'a' in map mp.
cout<<"Number of elements with key = 'a' in map mp are : "<<mp.count('a')<<endl;
//if mp is empty then itmp.empty will return 1 else 0.
if(mp.empty())
{
cout<<"Map is empty"<<endl;
}
else
{
cout<<"Map is not empty"<<endl;
}
return 0;
}
xxxxxxxxxx
#include <iostream>
#include <string>
#include <map>
using std::string;
using std::map;
map <int,string>MapName;//you can put type you want
map<int,string>::iterator iter;
int main(){
int i=31136;
string s="name;
//How you insert values
MapName.insert(pair<int,string>(i,s));
//How you print keys and values
for(iter=studentId.begin();iter!=studentId.end();iter++){
cout<<"Key"<<iter->first<<"Value: "<<iter->second<<endl;
}
//How you search data
if (m.find(31136) != m.end()) {
cout << "found" << endl;
}
else {
cout << "not found" << endl;
}
}
xxxxxxxxxx
//begin() – Returns an iterator to the first element in the map.
//end() – Returns an iterator to the theoretical element that follows the last element in the map.
//size() – Returns the number of elements in the map.
//max_size() – Returns the maximum number of elements that the map can hold.
//empty() – Returns whether the map is empty.
//pair insert(keyvalue, mapvalue) – Adds a new element to the map.
//erase(iterator position) – Removes the element at the position pointed by the iterator.
//erase(const g)– Removes the key-value ‘g’ from the map.
//clear() – Removes all the elements from the map