[关闭]
@zhengyuhong 2015-06-09T09:31:19.000000Z 字数 4999 阅读 1490

unordered_map

C++11 STL Boost


unordered_map

  1. template < class Key, // unordered_map::key_type
  2. class T, // unordered_map::mapped_type
  3. class Hash = hash<Key>, // unordered_map::hasher
  4. class Pred = equal_to<Key>, // unordered_map::key_equal
  5. class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type
  6. > class unordered_map;

  std::unordered_mapstd::map的区别就是,std::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合适的位置插入到树中。所以,如果对map进行遍历(中序遍历)的话,输出的结果是有序的。顺序就是按照operator< 定义的大小排序。而std::unordered_map是计算元素的Hash值,根据Hash值判断元素是否相同。所以,对std::unordered_map进行遍历,结果是无序的。
  用法的区别就是,std::map 的key需要定义operator< 。 而std::unordered_map需要定义hash_value函数并且重载operator==。对于内置类型,如std::string,这些都不用操心。对于自定义的类型做key,就需要自己重载operator< 或者hash_value()了。
  最后,说,当不需要结果排好序时,最好用std::unordered_map

unordered_map::unordered_map

  1. explicit unordered_map ( size_type n = /* see below */,
  2. const hasher& hf = hasher(),
  3. const key_equal& eql = key_equal(),
  4. const allocator_type& alloc = allocator_type() );
  5. explicit unordered_map ( const allocator_type& alloc );
  6. template <class InputIterator>
  7. unordered_map ( InputIterator first, InputIterator last,
  8. size_type n = /* see below */,
  9. const hasher& hf = hasher(),
  10. const key_equal& eql = key_equal(),
  11. const allocator_type& alloc = allocator_type() );
  12. unordered_map ( initializer_list<value_type> il,
  13. size_type n = /* see below */,
  14. const hasher& hf = hasher(),
  15. const key_equal& eql = key_equal(),
  16. const allocator_type& alloc = allocator_type() );

example

  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4. typedef std::unordered_map<std::string,std::string> stringmap;
  5. stringmap merge (stringmap a,stringmap b) {
  6. stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
  7. }
  8. int main ()
  9. {
  10. stringmap first; // empty
  11. stringmap second ( {{"apple","red"},{"lemon","yellow"}} ); // init list
  12. stringmap third ( {{"orange","orange"},{"strawberry","red"}} ); // init list
  13. stringmap fourth (second); // copy
  14. stringmap fifth (merge(third,fourth)); // move
  15. stringmap sixth (fifth.begin(),fifth.end()); // range
  16. std::cout << "sixth contains:";
  17. for (auto& x: sixth) std::cout << " " << x.first << ":" << x.second;
  18. std::cout << std::endl;
  19. return 0;
  20. }

unordered_map::at

  1. mapped_type& at ( const key_type& k );
  2. const mapped_type& at ( const key_type& k ) const;

unordered_map::operator[]

  1. mapped_type& operator[] ( const key_type& k );
  2. mapped_type& operator[] ( key_type&& k );

unordered_map::bucket

  1. size_type bucket ( const key_type& k ) const;
  2. //Locate element's bucket
  3. size_type bucket_count() const noexcept;
  4. //return number of buckets
  5. size_type bucket_size ( size_type n ) const;
  6. //returns the number of elements in bucket n.

unordered_map::count

  1. size_type count ( const key_type& k ) const;
  2. //count elements with a specific key

unordered_map::emplace

  1. template <class... Args>
  2. pair<iterator, bool> emplace ( Args&&... args );
  3. //就地构造新的元素<k,v>,如果k尚未在unorder_map中出现,即插入<k,v>

example

  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4. int main ()
  5. {
  6. std::unordered_map<std::string,std::string> mymap;
  7. mymap.emplace ("NCC-1701", "J.T. Kirk");
  8. mymap.emplace ("NCC-1701-D", "J.L. Picard");
  9. mymap.emplace ("NCC-74656", "K. Janeway");
  10. std::cout << "mymap contains:" << std::endl;
  11. for (auto& x: mymap)
  12. std::cout << x.first << ": " << x.second << std::endl;
  13. std::cout << std::endl;
  14. return 0;
  15. }

unordered_map::emplace_hint

  1. template <class... Args>
  2. iterator emplace_hint ( const_iterator position, Args&&... args );

emplace的功能基本一致,就地生成新元素,若k尚未出现在unordered_map中即插入,插入新元素时有一个hint提示位置,告诉函数从给定的位置查找的插入位置,这样可以提高速度,但是hint给出的迭代器可能不能使得优化插入速度,函数就不会使用。通常还是使用上述的emplace即可。

unordered_map::insert

  1. pair<iterator,bool> insert ( const value_type& val );
  2. //typedef pair<const Key, T> value_type;
  3. template <class P>
  4. pair<iterator,bool> insert ( P&& val );
  5. iterator insert ( const_iterator hint, const value_type& val );
  6. template <class P>
  7. iterator insert ( const_iterator hint, P&& val );
  8. template <class InputIterator>
  9. void insert ( InputIterator first, InputIterator last );
  10. void insert ( initializer_list<value_type> il );

example

  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_map>
  4. int main ()
  5. {
  6. std::unordered_map<std::string,double>
  7. myrecipe,
  8. mypantry = {{"milk",2.0},{"flour",1.5}};
  9. std::pair<std::string,double> myshopping ("baking powder",0.3);
  10. myrecipe.insert (myshopping); // copy insertion
  11. myrecipe.insert (std::make_pair<std::string,double>("eggs",6.0)); // move insertion
  12. myrecipe.insert (mypantry.begin(), mypantry.end()); // range insertion
  13. myrecipe.insert ( {{"sugar",0.8},{"salt",0.1}} ); // initializer list insertion
  14. std::cout << "myrecipe contains:" << std::endl;
  15. for (auto& x: myrecipe)
  16. std::cout << x.first << ": " << x.second << std::endl;
  17. std::cout << std::endl;
  18. return 0;
  19. }

unordered_map::public member function

  1. iterator begin() noexcept;
  2. const_iterator begin() const noexcept;
  1. local_iterator begin ( size_type n );
  2. const_local_iterator begin ( size_type n ) const;
  1. iterator end() noexcept;
  2. const_iterator end() const noexcept;
  1. local_iterator end (size_type n);
  2. const_local_iterator end (size_type n) const;
  1. const_iterator cbegin() const noexcept;
  2. const_local_iterator cbegin ( size_type n ) const;
  1. const_iterator cend() const noexcept;
  2. const_local_iterator cend ( size_type n ) const;
  1. iterator erase ( const_iterator position );
  2. size_type erase ( const key_type& k );
  3. iterator erase ( const_iterator first, const_iterator last );
  1. iterator find ( const key_type& k );
  2. const_iterator find ( const key_type& k ) const;
  1. void rehash( size_type n ); //set number of buckets
  2. void reserve ( size_type n ); //request a capacity change
  3. size_type size() const noexcept; //return container size
  1. bool empty() const noexcept
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注