#include<iostream>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<char,int> hash_table;
// 每次添加都是头插
hash_table['a'] = 9;
hash_table['?'] = 5;
hash_table['z'] = 99;
hash_table['A'] = 50;
hash_table['Z'] = -2;
hash_table[' '] = 0;
// 输出元素的数量
cout<<"元素的数量为:"<<hash_table.size()<<endl;
// 判断是否有这个key
if(hash_table.count(' ')==1)
{
cout<<hash_table[' ']<<endl;
}
// 输出内部所有的元素
for(auto it = hash_table.begin();it != hash_table.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
结果如下: