c++11 标准模板(STL)(std::unordered_multiset)(二)
创始人
2025-05-30 12:10:30
0
定义于头文件 
template<

    class Key,
    class Hash = std::hash,
    class KeyEqual = std::equal_to,
    class Allocator = std::allocator

> class unordered_multiset;
(1)(C++11 起)
namespace pmr {

    template               class Hash = std::hash,
              class Pred = std::equal_to>
    using unordered_multiset = std::unordered_multiset                                    std::pmr::polymorphic_allocator>

}
(2)(C++17 起)

unordered_multiset 是关联容器,含有可能非唯一 Key 类型对象的集合。搜索、插入和移除拥有平均常数时间复杂度。

元素在内部并不以任何顺序排序,只是被组织到桶中。元素被放入哪个桶完全依赖其值的哈希。这允许快速访问单独的元素,因为一旦计算哈希,它就指代放置该元素的准确的桶。

不要求此容器的迭代顺序稳定(故例如 std::equal 不能用于比较二个 std::unordered_multiset ),除了关键比较等价(以 key_eq() 为比较器比较相等)的每组元素组成迭代顺序中的相接子范围,它可用 equal_range() 访问。

构造与析构

构造 unordered_multiset

std::unordered_multiset::unordered_multiset
unordered_multiset() : unordered_multiset( size_type(/*implementation-defined*/) ) {}

explicit unordered_multiset( size_type bucket_count,
                             const Hash& hash = Hash(),
                             const key_equal& equal = key_equal(),

                             const Allocator& alloc = Allocator() );
(1)(C++11 起)
unordered_multiset( size_type bucket_count,

                    const Allocator& alloc )
                   : unordered_multiset(bucket_count, Hash(), key_equal(), alloc) {}
unordered_multiset( size_type bucket_count,
                    const Hash& hash,
                    const Allocator& alloc )

                   : unordered_multiset(bucket_count, hash, key_equal(), alloc) {}
(1)(C++14 起)

explicit unordered_multiset( const Allocator& alloc );

(1)(C++11 起)
template< class InputIt >

unordered_multiset( InputIt first, InputIt last,
                    size_type bucket_count = /*implementation-defined*/,
                    const Hash& hash = Hash(),
                    const key_equal& equal = key_equal(),

                    const Allocator& alloc = Allocator() );
(2)(C++11 起)
template< class InputIt >

unordered_multiset( InputIt first, InputIt last,
                    size_type bucket_count,
                    const Allocator& alloc )
                   : unordered_multiset(first, last,

                       bucket_count, Hash(), key_equal(), alloc) {}
(2)(C++14 起)
template< class InputIt >

unordered_multiset( InputIt first, InputIt last,
                    size_type bucket_count,
                    const Hash& hash,
                    const Allocator& alloc )
                   : unordered_multiset(first, last,

                       bucket_count, hash, key_equal(), alloc) {}
(2)(C++14 起)

unordered_multiset( const unordered_multiset& other );

(3)(C++11 起)

unordered_multiset( const unordered_multiset& other, const Allocator& alloc );

(3)(C++11 起)

unordered_multiset( unordered_multiset&& other );

(4)(C++11 起)

unordered_multiset( unordered_multiset&& other, const Allocator& alloc );

(4)(C++11 起)
unordered_multiset( std::initializer_list init,

                    size_type bucket_count = /*implementation-defined*/,
                    const Hash& hash = Hash(),
                    const key_equal& equal = key_equal(),

                    const Allocator& alloc = Allocator() );
(5)(C++11 起)
unordered_multiset( std::initializer_list init,

                    size_type bucket_count,
                    const Allocator& alloc )
                   : unordered_multiset(init, bucket_count,

                       Hash(), key_equal(), alloc) {}
(5)(C++14 起)
unordered_multiset( std::initializer_list init,

                    size_type bucket_count,
                    const Hash& hash,
                    const Allocator& alloc )
                   : unordered_multiset(init, bucket_count,

                       hash, key_equal(), alloc) {}
(5)(C++14 起)

从各种数据源构造新容器。可选的以用户提供的 bucket_count 为用于创建的最小桶数,以 hash 为哈希函数,以 equal 为比较关键的函数,和以 alloc 为分配器。

1) 构造空容器。设置 max_load_factor() 为 1.0 。对于默认构造函数,桶数是实现定义的。

2) 构造拥有范围 [first, last) 的内容的容器。设置 max_load_factor() 为 1.0 。

3) 复制构造函数。构造拥有 other 内容副本的容器,一同复制加载因子、谓词和哈希函数。若不提供 alloc ,则通过调用 std::allocator_traits::select_on_container_copy_construction(other.get_allocator()) 获得分配器。

4) 移动构造函数。用移动语义构造拥有 other 内容的容器。若不提供 alloc ,则通过从属于 other 的分配器移动构造获得分配器。

5) 构造拥有 initializer_list init 内容的容器,同 unordered_multiset(init.begin(), init.end()) 。

参数

alloc-用于此容器所有内存分配器的分配器
bucket_count-初始化时用的最小桶数。若不指定,则使用实现定义的默认值
hash-要用的哈希函数
equal-用于此容器所有关键比较的比较函数
first, last-复制元素来源的范围
other-用作源以初始化容器元素的另一容器
init-用以初始化容器元素的 initializer_list
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

复杂度

1) 常数

2) 平均情况与 firstlast 间的距离成线性,最坏情况成平方。

3) 与 other 的大小成线性。

4) 常数。若给定 alloc 且 alloc != other.get_allocator() 则为线性。

5) 平均情况与 init 的大小成线性,最坏情况成平方。

异常

Allocator::allocate 的调用可能抛出。

注意

在容器移动构造(重载 (4) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。

析构 unordered_multiset

std::unordered_multiset::~unordered_multiset

~unordered_multiset();

(C++11 起)

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

调用示例

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include using namespace std;struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator +(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator *(const Cell &cell){x *= cell.x;y *= cell.y;return *this;}Cell &operator ++(){x += 1;y += 1;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}bool operator >(const Cell &cell) const{if (x == cell.x){return y > cell.y;}else{return x > cell.x;}}bool operator ==(const Cell &cell) const{return x == cell.x && y == cell.y;}
};struct myCompare
{bool operator()(const int &a, const int &b){return a < b;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}std::ostream &operator<<(std::ostream &os, const std::pair &pCell)
{os << pCell.first << "-" << pCell.second;return os;
}struct CHash
{size_t operator()(const Cell& cell) const{size_t thash = std::hash()(cell.x) | std::hash()(cell.y);
//        std::cout << "CHash: " << thash << std::endl;return thash;}
};struct CEqual
{bool operator()(const Cell &a, const Cell &b) const{return a.x == b.x && a.y == b.y;}
};int main()
{std::cout << std::boolalpha;std::mt19937 g{std::random_device{}()};srand((unsigned)time(NULL));auto generate = [](){int n = std::rand() % 10 + 110;Cell cell{n, n};return cell;};//1) 构造空容器。设置 max_load_factor() 为 1.0 。对于默认构造函数,桶数是实现定义的。std::unordered_multiset unordered_multiset1;std::cout << "unordered_multiset1 is empty " << unordered_multiset1.empty() << std::endl;std::cout << std::endl;std::vector vector1(6);std::generate(vector1.begin(), vector1.end(), generate);std::cout << "vector1:              ";std::copy(vector1.begin(), vector1.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl;//2) 构造拥有范围 [first, last) 的内容的容器。//设置 max_load_factor() 为 1.0 。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的。std::unordered_multiset unordered_multiset2(vector1.begin(), vector1.end());std::cout << "unordered_multiset2:  ";std::copy(unordered_multiset2.begin(), unordered_multiset2.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl;//3) 复制构造函数。构造拥有 other 内容副本的容器,一同复制加载因子、谓词和哈希函数。std::unordered_multiset unordered_multiset3(unordered_multiset2);std::cout << "unordered_multiset3:  ";std::copy(unordered_multiset3.begin(), unordered_multiset3.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl;//4) 移动构造函数。用移动语义构造拥有 other 内容的容器。//若不提供 alloc ,则通过从属于 other 的分配器移动构造获得分配器。std::unordered_multiset unordered_multiset4(std::move(unordered_multiset2));std::cout << "unordered_multiset4:  ";std::copy(unordered_multiset4.begin(), unordered_multiset4.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl;//5) 构造拥有 initializer_list init 内容的容器,同 unordered_multiset(init.begin(), init.end()) 。std::unordered_multiset unordered_multiset5{generate(), generate(), generate(), generate(), generate(), generate()};std::cout << "unordered_multiset5:  ";std::copy(unordered_multiset5.begin(), unordered_multiset5.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl;return 0;
}

输出

相关内容

热门资讯

走进小城看消费丨江西资溪:低碳...   夏日时节下午4点,江西省抚州市资溪县大觉山景区漂流终点依然热闹。来自南昌的游客余鑫漂流结束后没有...
【中原晨会0625】市场分析专... 来源:市场资讯 (来源:中原证券研究所) 本期重点研报目录 【中原策略】市场分析:电子半导体领涨 ...
南向资金连买4日!低费率+可月... 6月25日早盘,港股红利资产震荡整理。截至11时14分,港股红利低波ETF招商(520550)下跌0...
618成交破百万!紫荆花用一套... 一年一度的618年中大促,是消费市场的晴雨表,也是品牌间最激烈的角力场。当各大品牌在直播间里铆足了劲...
原创 黄... 2026年6月25日的国际金价已经从前期的5500美元高点跌到4200美元下方,累计跌幅超过22%,...
英伟达CEO:Vera Rub... 截至9:38,中证半导体材料设备主题指数(931743)涨2.36%创新高;权重股中,中微公司涨3....
再被催债16亿!“钢铁大王”戴... 澎湃新闻记者 贺梨萍 因“铁本事件”入狱五年的戴国芳重返钢铁行业,但他并没有完成从阶下囚再到“钢铁大...
周三原油价格下跌 随着美国和伊朗在和平谈判中取得进展,越来越多的油轮公开穿越霍尔木兹海峡,原油在战时的价格上涨已经蒸发...
这种蛋白是大脑衰老的开关 这种蛋白是大脑衰老的开关 清晨,假设一位五十岁左右的王女士发现自己常常把手机放在熟悉的抽屉里又找不到...
信通院牵头算力Token出海生... 盘面上,截至11:04,中证科创创业50指数(931643)涨1.68%,创历史新高;权重股中,芯原...
海外 774 亿营收背后:日本... 文 | 游戏价值论 6月23日,彭博社报道了腾讯正在围绕出售多家日本游戏工作室少数股权开展谈判,包...
餐饮“抢人”大战:把店开到公交... 作者 |餐饮老板内参 内参君 医院、公交站、演唱会…餐饮品牌,正在无孔不入 在北京儿童医院,肯德基...
快讯 | 外资扫货!陈翊庭:港... 港交所行政总裁陈翊庭在接受《中国证券报》专访时指出,国际资本对中国资产的看法已彻底扭转,布局中国市场...
2777.77元!A股“股王”... 25日早盘,昨天创下历史新高的A股“股王”联讯仪器,今天上午继续走强,盘中股价再度刷新历史新高。 截...
原创 今... 欧洲自己的媒体直接下结论,欧盟衰退躲不掉,内部分裂拦不住,现在就连欧洲顶尖工业巨头,都偷偷在用中国的...
黄仁勋股东大会放言:本轮AI基... 在当地时间6月24日的英伟达(NVDA.O)2026年度股东大会上,股东批准了该公司全部10名董事会...
国际油价大跌 新华社消息, 纽约原油期货主力合约价格24日盘中跌破每桶70美元,为伊朗战事爆发以来首次。 市场分析...
马云带队插秧,什么信号? 一场别开生面的“务农”,让外界看到了一个不一样的阿里巴巴。 近日,阿里巴巴合伙人、高德董事长刘振飞在...
全球最大产能,最高丰度达99.... 本文转自【科技日报】; 6月23日,高丰度硼-10同位素技术暨产业化成果发布会在山东省东营市举办,全...
黄金大跳水!金饰克价年内暴跌近... 25日,现货黄金盘中震荡,截至发稿,报3985.070美元/盎司,跌0.17%。 当地时间24日,...