#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
// 声明
map<int, string> m_map;
// 插入元素
m_map.insert(make_pair(1, "abc"));
m_map.insert(pair<int,string>(2,"bcd"));
// 迭代器
map<int, string>::iterator ite;
for (ite=m_map.begin();ite!=m_map.end();ite++)
{
int key = ite->first;
string value = ite->second;
cout << key << "," << value << endl;
}
// map大小
int size = m_map.size();
cout << size << endl;
// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
ite = m_map.find(1);
cout << ite->second << endl;
//迭代器刪除
ite = m_map.find(1);
m_map.erase(ite);
//用关键字刪除
int n = m_map.erase(1); //如果刪除了會返回1,否則返回0
// map 是否为空
bool stat = m_map.empty();
system("pause");
return 0;
}
Map容器常用函数
最新推荐文章于 2024-02-04 17:01:36 发布