unordered_set和unordered_map基本使用

本文介绍了STL中unordered_set和unordered_map的基本使用,包括构造方式、赋值拷贝、迭代器操作、判断空、元素操作、插入删除以及容器管理等核心功能。
摘要由CSDN通过智能技术生成

unordered_set和unordered_map基本使用


STL提供了4个unordered系列关联式容器,分别是:unordered_set、unordered_map、unordered_multiset、unordered_multimap,这4个容器的使用与底层为红黑树的关联式容器是十分类似的,只不过现在这4个容器的底层机构都是哈希结构,因此其查询效率比底层为红黑树的关联式容器的要高一些。我们这里只介绍unordered_set和unordered_map的基本使用,unordered_multiset和unordered_multimap的使用都是类似的,自行查看文档即可。

unordered_set

template < class Key,                        // unordered_set::key_type/value_type
           class Hash = hash<Key>,           // unordered_set::hasher
           class Pred = equal_to<Key>,       // unordered_set::key_equal
           class Alloc = allocator<Key>      // unordered_set::allocator_type
           > class unordered_set;

在这里插入图片描述
1.构造

//empty (1)	
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 );


//initializer list (2)	
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() );
          
                         
//copy (3)	
unordered_set ( const unordered_set& ust );
unordered_set ( const unordered_set& ust, const allocator_type& alloc );


//move (4)	
unordered_set ( unordered_set&& ust );
unordered_set ( unordered_set&& ust, const allocator_type& alloc );

//range (5)	
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() );

可以参考以下例子:

  std::unordered_set<std::string> first;                                // empty
  std::unordered_set<std::string> second ( {"red","green","blue"} );    // init list
  std::unordered_set<std::string> third ( second );                    // copy
  std::unordered_set<std::string> fourth ( cmerge(third,fourth) );       // move
  std::unordered_set<std::string> fifth ( fifth.begin(), fifth.end() ); // range

2.赋值拷贝

copy (1)
unordered_set& operator= ( const unordered_set& ust );


move (2)	
unordered_set& operator= ( unordered_set&& ust );

//
initializer list (3)	
unordered_set& operator= ( intitializer_list<value_type> il );

可以参考以下例子

  std::unordered_set<std::string> first, second, third;
  
  first = {"red","green","blue"};      // init list
  second= cmerge (first, second);      // move
  third = third;                       // copy

3.迭代器

//container iterator (1)	
      iterator begin() noexcept;
const_iterator begin() const noexcept;

      iterator end() noexcept;
const_iterator end() const noexcept;

const_iterator cbegin() const noexcept;

const_iterator cend() const noexcept;


//bucket iterator (2)	
//返回哈希桶下标为n的迭代器
      local_iterator begin ( size_type n );
const_local_iterator begin ( size_type n ) const;

      local_iterator end (size_type n);
const_local_iterator end (size_type n) const;

const_local_iterator cbegin ( size_type n ) const;

const_local_iterator cend ( size_type n ) const;

4.判断是否为空

bool empty() const noexcept;

5.获取元素个数

size_type size() const noexcept;

6.查找值为K的元素

      iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;

6.数据插入

pair<iterator,bool> insert ( const value_type& val );
pair<iterator,bool> insert ( value_type&& val );

//建议一个起始的插入位置hint
iterator insert ( const_iterator hint, const value_type& val );
iterator insert ( const_iterator hint, value_type&& val );

template <class InputIterator>
    void insert ( InputIterator first, InputIterator last );

void insert ( initializer_list<value_type> il );

7.元素删除

iterator erase ( const_iterator position );

size_type erase ( const key_type& k );

iterator erase ( const_iterator first, const_iterator last );

8.清空

void clear() noexcept;

9.交换两个容器

void swap ( unordered_set& ust );

10.获取哈希桶个数

size_type bucket_count() const noexcept;

11.获取第n个哈希桶的元素个数

size_type bucket_size ( size_type n ) const;

12.返回K所在的哈希桶的下标(从0开始)

size_type bucket ( const key_type& k ) const;

13.获取负载因子

float load_factor() const noexcept;

14.重置哈希桶个数

void rehash ( size_type n );

15.为n个元素预留适合的哈希桶个数

void reserve ( size_type n );

16.返回当前容器所用的哈希函数

hasher hash_function() const;

unordered_map

template < class Key,                                    // unordered_map::key_type
           class T,                                      // unordered_map::mapped_type
           class Hash = hash<Key>,                       // unordered_map::hasher
           class Pred = equal_to<Key>,                   // unordered_map::key_equal
           class Alloc = allocator< pair<const Key,T> >  // unordered_map::allocator_type
           > class unordered_map;

在这里插入图片描述
1.构造

//empty (1)	
explicit unordered_map ( size_type n = /* see below */,
                         const hasher& hf = hasher(),
                         const key_equal& eql = key_equal(),
                         const allocator_type& alloc = allocator_type() );
explicit unordered_map ( const allocator_type& alloc );


//initializer list (2)	
unordered_map ( 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() );

//range (3)	
template <class InputIterator>
  unordered_map ( 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() );

//copy (4)	
unordered_map ( const unordered_map& ump );
unordered_map ( const unordered_map& ump, const allocator_type& alloc );


//move (5)	
unordered_map ( unordered_map&& ump );
unordered_map ( unordered_map&& ump, const allocator_type& alloc );

具体使用可以参考以下例子:

  typedef std::unordered_map<std::string,std::string> stringmap;
  
  stringmap first;                              // empty
  
  stringmap second ( {{"apple","red"},{"lemon","yellow"}} );       // init list

  stringmap third (second);                    // copy
  
  stringmap fourth (merge(third,fourth));        // move
  
  stringmap fifth (fifth.begin(),fifth.end());  // range

2.赋值拷贝

//initializer list (1)	
unordered_map& operator= ( intitializer_list<value_type> il );

//copy (2)	
unordered_map& operator= ( const unordered_map& ump );

//move (3)	
unordered_map& operator= ( unordered_map&& ump );

具体使用可以参考以下例子:

  typedef std::unordered_map<std::string,std::string> stringmap;

  stringmap first, second, third;
  
  first = {{"AAPL","Apple"},{"MSFT","Microsoft"}};  // init list
  
  second = first;                                    // copy
  
  third = merge(first,second);                      // move

3.迭代器

//container iterator (1)	
      iterator begin() noexcept;
const_iterator begin() const noexcept;

      iterator end() noexcept;
const_iterator end() const noexcept;

const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;

//bucket iterator (2)	
//返回下标为n的哈希桶的迭代器
      local_iterator begin ( size_type n );
const_local_iterator begin ( size_type n ) const;

      local_iterator end (size_type n);
const_local_iterator end (size_type n) const;

const_local_iterator cbegin ( size_type n ) const;
const_local_iterator cend ( size_type n ) const;

4.判断是否为空

bool empty() const noexcept;

5.获取数据个数

size_type size() const noexcept;

6.方括号[]下标访问

mapped_type& operator[] ( const key_type& k );
mapped_type& operator[] ( key_type&& k );

如果K存在,则返回对应的value值,如果K不存在,则插入key值为K的数据,对应value调用其默认构造函数。

7.使用at方法进行元素访问

mapped_type& at ( const key_type& k );
const mapped_type& at ( const key_type& k ) const;

如果K存在,则返回对应的value值,如果K不存在,抛异常终止程序

8.查找某个元素并返回其迭代器

      iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;

9.数据插入

//(1)	
pair<iterator,bool> insert ( const value_type& val );

//(2)	
template <class P>
    pair<iterator,bool> insert ( P&& val );
    
//(3)
//建议一个起始的插入位置hint
iterator insert ( const_iterator hint, const value_type& val );

//(4)	
template <class P>
    iterator insert ( const_iterator hint, P&& val );

//(5)	
template <class InputIterator>
    void insert ( InputIterator first, InputIterator last );

//(6)	
void insert ( initializer_list<value_type> il );

10.数据删除

iterator erase ( const_iterator position );

size_type erase ( const key_type& k );

iterator erase ( const_iterator first, const_iterator last );

11.数据清空

void clear() noexcept;

12.交换两个容器

void swap ( unordered_map& ump );

13.获取哈希桶个数

size_type bucket_count() const noexcept;

14.获取第n个哈希桶的元素个数

size_type bucket_size ( size_type n ) const;

15.返回key值为K的元素所在的哈希桶下标(从0开始)

size_type bucket ( const key_type& k ) const;

16.返回当前对象的负载因子

float load_factor() const noexcept;

17.将当前对象的哈希桶个数设为n

void rehash( size_type n )

18.为n个元素预留适合的哈希桶个数

void reserve ( size_type n );

19.返回当前对象的哈希函数

hasher hash_function() const;

关于unordered系列的容器的具体使用可以直接查看相关文档:
unordered_set
unordered_map
unordered_multiset
unordered_multimap

  • 17
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值