#include<iostream>
#include<map>
using namespace std;
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, 20));
m.insert(pair<int, int>(3, 30));
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "查找到了元素 key = " << (*pos).first << " value = " << pos->second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
//map不允许插入重复key元素,count统计而言,结果要么是0 要么是1
//multimap的count统计可能大于1
int num = m.count(3);
cout << "num = " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
04_map容器_查找和统计
最新推荐文章于 2024-01-31 11:32:10 发布