map是关联容器,存储由键值和映射值的组合形成的元素,并遵循特定顺序。
1、构造
std::map<char,int> first;
std::map<char,int> second (first.begin(),first.end());
std::map<char,int> third (second);
std::map<char,int> myMap = first;
示例:
void baseTest()
{
std::map<char,int> first;
std::map<char,int> myMap;
first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;
myMap = first;
std::map<char,int> second (first.begin(),first.end());
std::map<char,int> third (second);
std::map<char,int,classcomp> fourth; // class as Compare
bool(*fn_pt)(char,char) = fncomp;
std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare
std::cout << "first size:" << first.size() << "\n";
std::cout << "mymap size:" << first.size() << "\n";
std::cout << "second size:" << second.size() << "\n";
std::cout << "third size:" << third.size() << "\n";
std::cout << "fourth size:" << fourth.size() << "\n";
std::cout << "fifth size:" << fifth.size() << "\n";
}
运行结果:
first size:4
mymap size:4
second size:4
third size:4
fourth size:0
fifth size:0
2、Iterators
begin():返回第一个元素的迭代器
end():返回最末元素的迭代器
rbegin():返回Vector尾部的逆迭代器
rend():返回Vector起始的逆迭代器
cbegin():返回 const 的begin()
cend():返回 const 的end()
crbegin():返回 const 的 rbegin()
crend():返回 const 的 rend()
示例:
void IteratorsTest()
{
std::map<char,int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
std::cout << "mymap:\n";
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
std::cout << "reserve mymap:\n";
for (std::map<char,int>::reverse_iterator it=mymap.rbegin(); it!=mymap.rend(); ++it)
std::cout << it->first << " => " << it->second << '\n';
}
运行结果:
mymap:
a => 200
b => 100
c => 300
reserve mymap:
c => 300
b => 100
a => 200
3、Capacity:
size(): 返回元素个数
max_size(): 返回vector最大能放元素个数
empty():容器是否为空
示例:
void CapacityTest()
{
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
std::cout << "mymap.size() is " << mymap.size() << '\n';
std::cout << "mymap.max_size() is " << mymap.max_size() << '\n';
while (!mymap.empty())
{
std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
mymap.erase(mymap.begin());
}
}
运行结果:
mymap.size() is 3
mymap.max_size() is 461168601842738790
a => 10
b => 20
c => 30
4、Element access
operator[]: 同数组方式取值
at():返回第n个元素的引用
示例:
void ElementAccess()
{
std::map<char,std::string> mymap;
mymap['a']="an element";
mymap['b']="another element";
mymap['c']=mymap['b'];
std::cout << "mymap['a'] is " << mymap['a'] << '\n';
std::cout << "mymap['b'] is " << mymap['b'] << '\n';
std::cout << "mymap['c'] is " << mymap['c'] << '\n';
std::cout << "mymap['d'] is " << mymap['d'] << '\n';
std::cout << "mymap now contains " << mymap.size() << " elements.\n";
std::map<std::string,int> second = {
{ "alpha", 0 },
{ "beta", 0 },
{ "gamma", 0 } };
second.at("alpha") = 10;
second.at("beta") = 30;
second.at("gamma") = 20;
for (auto& x: second) {
std::cout << x.first << ": " << x.second << '\n';
}
}
运行结构:
mymap['a'] is an element
mymap['b'] is another element
mymap['c'] is another element
mymap['d'] is
mymap now contains 4 elements.
alpha: 10
beta: 30
gamma: 20
5、Modifiers
insert():插入一个元素 或系列元素,返回迭代器erase():删除某个元素或者系列元素,返回迭代器
swap():交换容器内容
clear():清楚容器内所有元素
emplace():插入一个元素,每次只能插入一个元素,而不是多个
emplace_hint():构造并插入带有提示的元素