#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');
#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;
}
}
map <typename,map<typename,typename>> mp;
map[key1][key2]=values
Input: nums = [1,2,3,1], k = 3, t = 0
Output: true