- map 是一种关联容器, 将键值和映射值组合成元素并且按照特定顺序进行排列, 通常键值用来排
序和唯一标识元素, 映射值用来存储和键值相关的内容; - 键值类型和映射值类型不要求一样, 但是通常声明为typedef pair
#include<iostream>
#include <map>
#include <string>
using namespace std;
template <class Key,
class T,
class Compare = less<Key>,
class Alloc = allocator<pair<const, Key, T>>
> class map;
bool fncomp(char lns, char rns)
{
return lns < rns;
}
struct clascomp
{
bool operator()(const char& lns, const char& rns)const
{
return lns < rns;
}
};
int main()
{
map<char, int> first;
first['a'] = 10;
first['b'] = 20;
first['c'] = 30;
first['d'] = 40;
map<char, int> second(first.begin(), first.end());
map<char, int> third(second);
map<char, int, clascomp> fourth;
bool(*fn_pt)(char, char) = fncomp;
map<char, int, bool(*)(char, char)> fifth(fn_pt);
map<string, int> mymap =
{
{"alpah", 0},
{"beta", 0},
{"gamma", 0}
};
mymap.at("alpah") = 10;
mymap.at("beta") = 20;
mymap.at("gamma") = 30;
cout << "mymap contains: ";
for (auto X : mymap)
cout << "(" << X.first << "," << X.second << ")" << " ";
cout << endl;
cout << "mymap contains: ";
for (map<string, int>::iterator It = mymap.begin(); It != mymap.end(); ++It)
cout << "(" << It->first + "lixun" << " => " << It->second + 10 << ")" << " ";
cout << endl;
cout << "mymap contains const: ";
for (auto It = mymap.cbegin(); It != mymap.cend(); ++It)
cout << "(" << It->first << " => " << It->second << ")" << " ";
cout << endl;
cout << "mymap contains reverse: ";
for (auto It = mymap.rbegin(); It != mymap.rend(); ++It)
cout << "(" << It->first + "lixun" << " => " << It->second + 10 << ")" << " ";
cout << endl;
cout << "mymap contains reverse: ";
for (auto It = mymap.crbegin(); It != mymap.crend(); ++It)
cout << "(" << It->first << " => " << It->second << ")" << " ";
cout << endl;
map<char, int> mymap1;
mymap1['a'] = 10;
mymap1['b'] = 9;
mymap1['c'] = 8;
char c;
for ( c = 'a'; c < 'g'; ++c)
{
cout << c << " ";
if (mymap1.count(c) > 0)
cout << "is an elememt of mymap1;" << endl;
else
cout << "is not an element of mymap1;" << endl;
}
map<char, int> mymap2;
mymap2.emplace('x', 1);
mymap2.emplace('y', 2);
mymap2.emplace('z', 3);
mymap2.emplace('k', 4);
cout << "mymap2 contains: ";
for (auto &X : mymap2)
cout << "(" << X.first << ":" << X.second << ")" << " ";
cout << endl;
{
auto It = mymap2.end();
It = mymap2.emplace_hint(It, 'b', 11);
mymap2.emplace_hint(It, 'c', 12);
It = mymap2.begin();
mymap2.emplace_hint(It, '3', 19);
cout << "mymap2.emplace_hint contains: ";
for (auto X : mymap2)
cout << "(" << X.first << ":" << X.second << ")" << " ";
cout << endl;
}
{
auto ret = mymap2.equal_range('b');
cout << "lower bound points to: ";
cout << ret.first->first << "=>" << ret.first->second << " ";
cout << endl;
cout << "upper bound points to: ";
cout << ret.second->first << "=>" << ret.second->second << " ";
cout << endl;
}
{
cout << "mymap2.erase() contains: ";
auto It = mymap2.find('b');
mymap2.erase(It);
mymap2.erase('x');
It = mymap2.find('k');
mymap2.erase(It,mymap2.end());
for(auto It=mymap2.begin();It!=mymap2.end();++It)
cout << It->first << "=>" << It->second << " ";
cout << endl;
}
{
mymap2['s']=12;
mymap2['v']=11;
mymap2['l']=10;
mymap2['j']=9;
mymap2['t']=0;
auto It = mymap2.find('j');
if(It!=mymap2.end())
mymap2.erase(It);
cout << "mymap2.erase() contains: ";
cout << "s=>" << mymap2.find('s')->second << " ";
cout << "l=>" << mymap2.find('l')->second << " ";
cout << "j=>" << mymap2.find('j')->second << " ";
cout << "t=>" << mymap2.find('t')->second << endl;
}
{
pair<const char,int> *p;
p=mymap2.get_allocator().allocate(5);
auto psize = sizeof(map<char,int>::value_type)*5;
cout << "The allocated array has sizeof " << psize << " bytes" << endl;
mymap2.get_allocator().deallocate(p,5);
}
{
map<char,int> mynewmap;
mynewmap.insert(pair<char,int>('a',101));
mynewmap.insert(pair<char,int>('b',102));
mynewmap.insert(pair<char,int>('c',103));
mynewmap.insert(pair<char,int>('c',104));
mynewmap.insert(pair<char,int>('d',105));
pair<map<char,int>::iterator,bool> ret;
mynewmap.insert(pair<char,int>('b',212));
if(ret.second==false){
cout << "element 'b' has already exist " << endl;
}else {
cout << "elemen 'b' inseret successful " << endl;
}
map<char,int>::iterator it = mynewmap.begin();
mynewmap.insert(it,pair<char,int>('b',300));
mynewmap.insert(it,pair<char,int>('c',200));
map<char,int> anothermap;
anothermap.insert(mynewmap.begin(),mynewmap.find('c'));
cout << "mynewmap contains: ";
for(auto X:mynewmap)
cout << X.first << " " << X.second << " ";
cout << endl;
for(auto X:anothermap)
cout << X.first << " " << X.second << " ";
cout << endl;
}
{
map<char,int> mymap;
map<char,int>::key_compare mycomp = mymap.key_comp();
mymap['a']=100;
mymap['b']=101;
mymap['c']=102;
cout << "mymap contains: ";
char highest =mymap.rbegin()->first;
map<char,int>::iterator it = mymap.begin();
do{
cout << it->first << "=>" << it->second << " ";
}while(mycomp((*it++).first,highest));
cout <<'\n';
}
{
map<char,int> mymap;
map<char,int>::iterator itlow,itup;
mymap['1']=16;
mymap['2']=19;
mymap['3']=20;
mymap['4']=19;
mymap['5']=17;
itlow = mymap.lower_bound('3');
itup = mymap.upper_bound('3');
mymap.erase(itlow,itup);
for(auto X:mymap){
cout << X.first << "=>" << X.second << " ";
}
cout << endl;
}
{
map<char,int> first;
map<char,int> second;
first['x']=1;
first['y']=2;
first['z']=3;
first['a']=4;
first['b']=5;
cout << "first contains: ";
for(auto X:first)
cout << "[" << X.first << ":" << X.second << " ";
cout << endl;
second = first;
for(auto X:second)
cout << "[" << X.first << ":" << X.second << " ";
cout << endl;
first = map<char,int>();
cout << first.size() << endl;
}
mymap1.swap(mymap1);
cout << "mymap1 contains: ";
for(auto X:mymap1)
cout << "[" << X.first << ":" << X.second << "] ";
cout << endl;
cout << "mymap2 contains: ";
for(auto X:mymap2)
cout << "[" << X.first << ":" << X.second << "] ";
cout << endl;
{
map<char,int> mymap;
mymap['a'] = 1001;
mymap['b'] = 1002;
mymap['c'] = 1003;
auto highest = *mymap.rbegin();
auto it = mymap.begin();
cout << "mymap value contains: ";
do{
cout << it->first << "=>" << it->second << " ";
}while(mymap.value_comp()(*it++,highest));
}
return 0;
}