#include <iostream>
#include <map>
#include <string>
using namespace std;
/*
关联容器之map
Key-Value一一对应 按照key的大小排序
*/
int main()
{
//创建空容器
map<unsigned int, string> m_map;
//插入一个元素
m_map.insert(pair<unsigned int , string>(1001, "苍井空"));
m_map[1002] = "小泽";
m_map[1003] = "明步";
//声明迭代器
map<unsigned int, string>::iterator iter_map;
//迭代器遍历
for (iter_map = m_map.begin(); iter_map != m_map.end(); ++iter_map) {
cout<<iter_map->first<<":"<<iter_map->second<<endl;
}
//删除元素
cout<<"------------------"<<endl;
m_map.erase(1001);
for (iter_map = m_map.begin(); iter_map != m_map.end(); ++iter_map) {
cout<<iter_map->first<<":"<<iter_map->second<<endl;
}
return 0;
}
关联容器之map
最新推荐文章于 2024-08-14 23:30:00 发布