#include <windows.h>
#include <hash_map>
#include <map>
using namespace std;
using namespace stdext;
typedef std::hash_map<int, string> _hash_map;
typedef std::pair<int, string> _hash_map_pair;
typedef std::hash_map<int, string>::iterator _hash_map_iterator;
typedef std::map<int, string> _map;
typedef std::map<int, string>::iterator _map_iterator;
int _tmain(int argc, _TCHAR* argv[])
{
//_hash_map tmpMap;
//tmpMap[1] = string("1");
//tmpMap[1] = string("2");
//tmpMap[1] = string("3");
//tmpMap.insert(_pair(1, string("1")));
//tmpMap.insert(_pair(1, string("2")));
//tmpMap.insert(_pair(1, string("3")));
//
_map tmpMap;
tmpMap.insert(_hash_map_pair(1, string("1")));
tmpMap.insert(_hash_map_pair(1, string("2")));
tmpMap.insert(_hash_map_pair(2, string("3")));
system("pause");
return 0;
}
2015-2-13 15:32:47更新:
std::map<int, int> tmpMap;
std::map<int, int>::iterator b;
tmpMap[1] = 1;
b = tmpMap.begin();
printf("map size is %d, first pair key-value is %d-%d.\n", tmpMap.size(), b->first, b->second);
tmpMap[1] = 2;
b = tmpMap.begin();
printf("map size is %d, first pair key-value is %d-%d.\n", tmpMap.size(), b->first, b->second);
以上代码的打印结果为:
map size is 1, first pair key-value is 1-1.
map size is 1, first pair key-value is 1-2.
请按任意键继续. . .
可以发现,在std::map的使用的时候,如果用下标的形式操作一组key-value,如果这组不存在,则进行插入操作,如果存在,则会更新。