美文网首页
STLRecipe---unordered_map

STLRecipe---unordered_map

作者: 世界上的一道风 | 来源:发表于2019-08-24 14:36 被阅读0次

unordered_map 包含的是有唯一键的键/值对元素。容器中的元素不是有序的。元素的位置由键的哈希值确定,因而必须有一个适用于键类型的哈希函数。

image.png
  • unordered_map初始化详解
1.
std::unordered_map<std::string, size_t> people {{"Jan",44}, {"Jim", 33}, {"Joe", 99}}; // Name,age

2.当我们知道要在容器中保存多少个元素时,可以在构造函数中指定应该分配的格子的个数:
std::unordered_map<std::string,size_t> people {{ { "Jan", 44}, {"Jim", 33}, {"Joe", 99}}, 10};

  • unordered_map插入元素
std:: unordered_map<std:: string, size_t> people { {"Jim", 33}, { "Joe", 99}};// Name,age
std::cout <<"people container has " << people.bucket_count()<<" buckets.\n"; // 8 buckets
auto pr = people.insert (std::pair<string, size_t> {"Jan", 44});// Move insert

这个插入操作不会成功,因为容器中已经有键值为 string("Jim") 的元素
std:: cout << "Element " << (pr.second ? "was" : "was not") << " inserted." << std::endl;

相关文章

  • STLRecipe---unordered_map

    unordered_map 包含的是有唯一键的键/值对元素。容器中的元素不是有序的。元素的位置由键的哈希值确定,因...

网友评论

      本文标题:STLRecipe---unordered_map

      本文链接:https://www.haomeiwen.com/subject/tcqqectx.html