* create
```
#include <map>
using namespace std;
struct classComp {
bool operator() (int l, int r) { return l<r; }
};
bool funComp(int l, int r) { return l<r; }
int main () {
map<int,int> a; // default constructor
map<int,int> b(a); // copy constructor
map<int,int> c(move(a)); // move constructor
map<int,int> d{{1,1},{2,1},{3,3},{1,1}}; // initializer list constructor
// map has no fill constructor
map<int,int> f(a.begin(),a.end()); // range constructor
map<int,int>* g = new map<int,int>{{1,1},{2,1},{3,3},{1,1}}; // new operator
delete g;
map<int,int,classComp> cc;
map<int,int,decltype(&funComp)> cf(&funComp);
}
```
* iterator
```
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int,int> m{{1,1},{2,1},{3,2},{1,4},{5,1}};
for (auto i = m.begin(); i != m.end(); ++i)
cout << i->first << "=>" << i->second << " ";
cout<<endl;
for (auto i = m.cbegin(); i != m.cend(); ++i)
cout << i->first << "=>" << i->second << " ";
cout<<endl;
for (auto i = m.rbegin(); i != m.rend(); ++i)
cout << i->first << "=>" << i->second << " ";
cout<<endl;
for (auto i = m.crbegin(); i != m.crend(); ++i)
cout << i->first << "=>" << i->second << " ";
cout<<endl;
}
/*
1=>1 2=>1 3=>2 5=>1
1=>1 2=>1 3=>2 5=>1
5=>1 3=>2 2=>1 1=>1
5=>1 3=>2 2=>1 1=>1
*/
```
* add
```
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<int,int> m;
m[1]=1;
m.insert(make_pair<int, int>(2,1));
m.emplace(make_pair<int, int>(3,1));
for (auto &i:m)
cout<<i.first<<"=>"<<i.second<<";";
cout<<endl;
}
// 1=>1;2=>1;3=>1;
```
* change
```
#include <iostream>
#include <map>
using namespace std;
void p(map<int,int>& a) {
for(auto &i:a) cout<<i.first<<"=>"<<i.second<<";";
cout<<endl;
}
int main ()
{
map<int,int> a{{1,1},{2,2},{3,3},{4,4},{1,5}};
map<int,int> a1(a), a2(a);
a.begin()->second=3;
a[1]=10;
a.at(2)=8;
a.swap(a1);
p(a);
}
```
* query
```
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> m{{1,1},{2,2},{3,2},{4,2}};
cout<<m.at(1)<<","<<m[2]<<endl;
if(auto it=m.find(2)!=m.end()) cout<<"found"<<endl;
cout<<m.count(2)<<endl;
auto it=m.begin();
auto itl = m.lower_bound(3);
while(it!=itl){
cout<<it->first<<"=>"<<it->second<<","; it++;
}
cout<<endl;
it=m.upper_bound(3);
while(it!=m.end()) {
cout<<it->first<<"=>"<<it->second<<","; it++;
}
cout<<endl;
auto r=m.equal_range(3);
for(auto it=r.first; it!=r.second; it++)
cout<<it->first<<"=>"<<it->second<<",";
cout<<endl;
cout<<m.empty()<<endl;
cout<<m.size()<<endl;
cout<<m.max_size()<<endl;
}
/*
1,2
found
1
1=>1,2=>2,
4=>2,
3=>2,
0
4
230584300921369395
*/
```
* delete
```
#include <map>
using namespace std;
int main() {
map<int,int> m{{1,1},{2,2},{3,1},{4,1},{5,1}};
m.erase(m.begin());
m.erase(2);
m.clear();
}
```