[关闭]
@zhengyuhong 2015-06-09T09:37:24.000000Z 字数 1615 阅读 1335

unordered_set

C++11 STL Boost


unordered_set

  1. template < class Key, // unordered_set::key_type/value_type
  2. class Hash = hash<Key>, // unordered_set::hasher
  3. class Pred = equal_to<Key>, // unordered_set::key_equal
  4. class Alloc = allocator<Key> // unordered_set::allocator_type
  5. > class unordered_set;

  std::unordered_setstd::set的区别可以参考unordered_map

unordered_set::unordered_set

  1. explicit unordered_set ( 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_set ( const allocator_type& alloc );
  6. template <class InputIterator>
  7. unordered_set ( 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_set ( 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() );

  unordered_setunordered_map接口基本一致,具体参考unordered_map即可。
example

  1. // constructing unordered_sets
  2. #include <iostream>
  3. #include <string>
  4. #include <unordered_set>
  5. template<class T>
  6. T cmerge (T a, T b) { T t(a); t.insert(b.begin(),b.end()); return t; }
  7. int main ()
  8. {
  9. std::unordered_set<std::string> first; // empty
  10. std::unordered_set<std::string> second ( {"red","green","blue"} ); // init list
  11. std::unordered_set<std::string> third ( {"orange","pink","yellow"} ); // init list
  12. std::unordered_set<std::string> fourth ( second ); // copy
  13. std::unordered_set<std::string> fifth ( cmerge(third,fourth) ); // move
  14. std::unordered_set<std::string> sixth ( fifth.begin(), fifth.end() ); // range
  15. std::cout << "sixth contains:";
  16. for (const std::string& x: sixth) std::cout << " " << x;
  17. std::cout << std::endl;
  18. return 0;
  19. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注