C++ std::unordered_multiset

std::unordered_multiset

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

Unordered multisets are containers that store elements in no particular order, allowing fast retrieval of individual elements based on their value, much like unordered_set containers, but allowing different elements to have equivalent values.

In an unordered_multiset, the value of an element is at the same time its key, used to identify it. Keys are immutable(不可变的), therefore, the elements in an unordered_multiset cannot be modified once in the container - they can be inserted and removed, though.

Internally, the elements in the unordered_multiset are not sorted in any particular, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their values (with a constant average time complexity on average).

Elements with equivalent values are grouped together in the same bucket and in such a way that an iterator (see equal_range) can iterate through all of them.

Iterators in the container are at least forward iterators.

Notice that this container is not defined in its own header, but shares header with unordered_set.

Example
#include <iostream>
#include <string>
#include <unordered_set>    ///> Note: use the unordered_set header

using namespace std;

template<class T>
T cmerge(T a, T b){
    T t(a);
    t.insert(b.begin(), b.end());
    return t;
}

int main(int argc, char **argv)
{
    unordered_multiset<string> first1;
    unordered_multiset<string> first2( {"one", "two", "three"} );
    unordered_multiset<string> first3( {"red", "green", "blue"} );
    unordered_multiset<string> first4( first2 );
    unordered_multiset<string> first5( cmerge(first4,first3) );
    unordered_multiset<string> first6( first5.begin(), first5.end() );

    cout << "\nFirst6 set: ";
    for(const string& x: first6 ){
        cout << "  " << x;
    }

    return 0;
}
Reference

cplusplus


转载于:https://www.cnblogs.com/zi-xing/p/6388721.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值