#include<iostream>
#include<map>
using namespace std;
#include<string>
//map容器查找和统计
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key:" << (*it).first << " value:" << it->second << endl;
}
cout << endl;
}
void test01()
{
//查找
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(3, 50));
map<int,int>::iterator pos = m.find(3); //返回的是迭代器
if (pos != m.end())
{
cout << "找到了 key:" << pos->first << " value:" << (*pos).second << endl;
}
else
{
cout << "未找到!" << endl;
}
cout << endl;
//统计
int num = m.count(3);
//map容器中 不允许插入重复的key元素,count结果要么是1要么是0
//multimap count统计可能大于1
//[]插入数据会覆盖,insert不会覆盖数据
cout << "num = " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.9.5 map查找和统计
最新推荐文章于 2024-07-18 21:33:10 发布