@zhengyuhong
2015-06-09T09:37:24.000000Z
字数 1615
阅读 1335
C++11 STL Boost
template < class Key, // unordered_set::key_type/value_typeclass Hash = hash<Key>, // unordered_set::hasherclass Pred = equal_to<Key>, // unordered_set::key_equalclass Alloc = allocator<Key> // unordered_set::allocator_type> class unordered_set;
std::unordered_set 与 std::set的区别可以参考unordered_map,
explicit unordered_set ( size_type n = /* see below */,const hasher& hf = hasher(),const key_equal& eql = key_equal(),const allocator_type& alloc = allocator_type() );explicit unordered_set ( const allocator_type& alloc );template <class InputIterator>unordered_set ( InputIterator first, InputIterator last,size_type n = /* see below */,const hasher& hf = hasher(),const key_equal& eql = key_equal(),const allocator_type& alloc = allocator_type() );unordered_set ( initializer_list<value_type> il,size_type n = /* see below */,const hasher& hf = hasher(),const key_equal& eql = key_equal(),const allocator_type& alloc = allocator_type() );
unordered_set与unordered_map接口基本一致,具体参考unordered_map即可。
example
// constructing unordered_sets#include <iostream>#include <string>#include <unordered_set>template<class T>T cmerge (T a, T b) { T t(a); t.insert(b.begin(),b.end()); return t; }int main (){std::unordered_set<std::string> first; // emptystd::unordered_set<std::string> second ( {"red","green","blue"} ); // init liststd::unordered_set<std::string> third ( {"orange","pink","yellow"} ); // init liststd::unordered_set<std::string> fourth ( second ); // copystd::unordered_set<std::string> fifth ( cmerge(third,fourth) ); // movestd::unordered_set<std::string> sixth ( fifth.begin(), fifth.end() ); // rangestd::cout << "sixth contains:";for (const std::string& x: sixth) std::cout << " " << x;std::cout << std::endl;return 0;}
