C++ 哈希map与STL map

#include <stdio.h>
#include<map>
#include<string>
struct ListNode
{
 std::string key;
 int val;
 ListNode* next;
 ListNode(int x) :val(x), next(NULL) {};
};
int main()
{
 std::map<std::string, int> hash_map;
 std::string str1 = "abc";
 std::string str2 = "aaa";
 std::string str3 = "xxx";
 hash_map[str1] = 1;
 hash_map[str2] = 2;
 hash_map[str3] = 100;
 if (hash_map.find(str1)!=hash_map.end())
 {
  printf("%s is in the hash_map, and the value is %d\n", str1.c_str(), hash_map[str1]);
 }
 std::map<std::string, int>::iterator it;
 for (it = hash_map.begin(); it != hash_map.end(); it++)
 {
  printf("hash_map[%s]=%d\n", it->first.c_str(), it->second);
 }
 return 0;
}

运行结果为:

abc is in the hash_map, and the value is 1
hash_map[aaa]=2
hash_map[abc]=1
hash_map[xxx]=100
发布了79 篇原创文章 · 获赞 62 · 访问量 2199

猜你喜欢

转载自blog.csdn.net/weixin_44208324/article/details/105012989
今日推荐