map
是有序键值对容器,它的元素的键是唯一的。用比较函数 Compare
排序键。搜索、移除和插入操作拥有对数复杂度。 map 通常实现为红黑树。
下面我们来学习他的基本操作,首先是创建,初始化,元素访问。
代码实现如下:
#include <iostream>
#include <map>
#include <string>
using namespace std;
void createMapInit()
{
//1.创建map
map<int, string> map1;
cout << "map1.size = " << map1.size() << " map1.empty = " << map1.empty() << " map1.max_size = " << map1.max_size() << endl;
map1[1] = "first";
map1[2] = "second";
map1[3] = "third";
cout << "map1.size = " << map1.size() << " map1.empty = " << map1.empty() << " map1.max_size = " << map1.max_size() << endl;
cout << "map1的值为:" << endl;
map<int, string>::iterator iter1;
for(iter1 = map1.begin(); iter1 != map1.end(); iter1++)
{
std::string tempStr = iter1->second;//这样可以
cout << iter1->first << ": " << iter1->second << endl;//为什么不能这样输出,加上头文件#include<string>才可以
//cout << iter1->first << ": " << iter1->second.c_str() << endl;
}
//2.列表初始化
map<int, string> map2 = {
{1, "RecordLib" },
{2, "ViewLib" },
{3, "EditLib" },
{4, "ControlLib"}
};
cout << "map2.size = " << map2.size() << " map2.empty = " << map2.empty() << " map2.max_size = " << map2.max_size() << endl;
cout << "map2的值为:" << endl;
for(auto &val: map2)
{
cout << val.first << ": " << val.second << endl;
}
//3.赋值初始化
map<int, string> map3 = map2;
map<int, string>::iterator iter3 = map3.begin();
cout << "map3.size = " << map3.size() << " map3.empty = " << map3.empty() << " map3.max_size = " << map3.max_size() << endl;
cout << "map3的值为:" << endl;
while (iter3 != map3.end())
{
cout << iter3->first << ": " << iter3->second << endl;
iter3++;
}
//4.拷贝初始化
map<int, string> map4(map1);
cout << "map4.size = " << map4.size() << " map4.empty = " << map4.empty() << " map4.max_size = " << map4.max_size() << endl;
auto iter4 = map4.begin();
cout << "map4的值为:" << endl;
while (iter4 != map4.end())
{
cout << iter4->first << ": " << iter4->second << endl;
iter4++;
}
//5.范围初始化
map<int, string> map5(map2.begin(), map2.end());
cout << "map5.size = " << map5.size() << " map5.empty = " << map5.empty() << " map5.max_size = " << map5.max_size() << endl;
auto iter5 = map5.begin();
cout << "map5的值为:" << endl;
while (iter5 != map5.end())
{
cout << iter5->first << ": " << iter5->second << endl;
iter5++;
}
//6.移动构造函数
map<int, string> map6(std::move(map1));
cout << "map6.size = " << map6.size() << " map6.empty = " << map6.empty() << " map6.max_size = " << map6.max_size() << endl;
auto iter6 = map6.begin();
cout << "map6的值为:" << endl;
while (iter6 != map6.end())
{
cout << iter6->first << ": " << iter6->second << endl;
iter6++;
}
//7.at(key)访问元素, 返回的是key对应的值
cout << "map6.at(1) = " << map6.at(1) << endl;
cout << "map6.at(2) = " << map6.at(2) << endl;
map6.at(1) = "scott";//修改1对应的值
map6.at(2) = "camle";//修改2对应的值
//8.[key]访问元素
cout << "map6[1] = " << map6[1] << endl;
cout << "map6[2] = " << map6[2] << endl;
cout << "map6的值为:" << endl;
for(auto &val: map6)
{
cout << val.first << ": " << val.second << endl;
}
cout << endl;
}
int main()
{
createMapInit();
cout << "Hello, world!" << endl;
return 0;
}
运行结果:
参考:
http://www.cplusplus.com/reference/map/map/
https://zh.cppreference.com/w/cpp/container/map