map的基本用法
- 变量声明
map<string ,string> m;
- 插入元素
m.insert(pair<string,string>("key","value"));
- 查找元素
iter = m.find("key");
if(iter != m.end())
cout<<"Find, the value is"<<iter->second<<endl;
else
cout<<"Do not Find"<<endl;
- 删除或者清空
int n = m.erase("key");
m.erase(m.begin(), m.end());
统计数组(或者向量)中各元素出现的次数
#include <map>
int main(void)
{
int i;
int a[] = { 1, 2, 3, 4, 5, 5, 5 };
map<int, int> m;
for (i = 0; i < 7; i++)
{
if (m.end() != m.find(a[i])) m[a[i]]++;
else m.insert(pair<int, int>(a[i], 1));
}
for (auto it : m)
{
cout << it.first << ends << it.second << endl;
}
return 0;
}