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;
}

输出

相关内容

热门资讯

路透解析“马斯克集团”:Spa... SpaceX 凤凰网科技讯 北京时间1月31日,据路透社报道,长期以来,埃隆·马斯克(Elon Mu...
启动“二改” 永辉在京完成21... 北京商报讯(记者 赵述评 实习记者 毛思怡)1月31日,永辉超市北京龙湖长楹天街店经一个多月闭店调改...
《宜宾散装白酒连锁经营规范》团... 近日,由宜宾市酒类协会牵头归口、宜宾安宁酒厂主导起草,四川谊宾酒业、宜宾学院、劲牌南溪酒业等多家本地...
印度牙医博士打造全印首款人形机... 2026 年 1 月 23 日,印度浦那的 Muks Robotics 正式宣布,自主研发的社交人形...
金银价创新高,引发全球“贵金属... 【环球时报记者 倪浩 环球时报特约记者 甄翔】连日来,国际市场金银价格持续大涨。1月29日当天,亚太...
财经观察丨“爱你老己”背后的消... 新华网北京1月31日电岁末年初,一句“爱你老己,明天见”席卷社交网络,成为年轻人自我关怀的新表达。热...
重磅!珠海科技产业集团与农行广... 1月30日,珠海科技产业集团与中国农业银行广东省分行在广州签署全面战略合作协议暨独立授信合作。农行广...
原创 黄... 谁能想到,2026年开年就上演金融魔幻现实主义! 国际黄金1月31日凌晨暴跌9.25%,盘中狂泻12...
云南省本级社会保险基金银行存款... 近日,云南省财政厅、云南省人力资源和社会保障厅、云南省医疗保障局联合印发《云南省本级社会保险基金银行...
病毒在身体里“安家”却相安无事... 很多人听说“乙肝携带者”,总会下意识和“乙肝患者”画上等号,担心自己或身边人被传染,也害怕携带者最终...
库迪确认:取消全场9.9元 来源:滚动播报 (来源:新消费日报) 有消息称,库迪咖啡发布门店价格策略和活动调整通知。通知指出,...
原创 雷... 不知道大家有没有发现,这个周六可能是进入2026年之后最消停的一个周六。因为各品牌基本上都没什么大事...
原创 特... 特朗普对委内瑞拉的举动,表面上看是一场能源棋局,实则背后隐藏着深刻的战略考量。对他而言,掌握能源就意...
原创 李... 01、“私募魔女”李蓓再引争议 半夏投资创始人、“私募魔女”李蓓,最近又成为投资圈的焦点。 1月2...
爱美客:AestheFill产... 上证报中国证券网讯(记者 王子霖)备受医美行业瞩目的AestheFill产品独家经销权纠纷迎来重要进...
雷军明晚直播,在北京小米汽车工... IT之家 1 月 31 日消息,今天午间,小米创办人、董事长兼 CEO 雷军在微博发文宣布,2 月 ...
字节阿里DeepSeek决战春... 新智元报道 编辑:艾伦 【新智元导读】这个春节,中国 AI 迎来「决战时刻」。据《The Info...
皇台酒业开始过年? 富凯摘要:有钱没钱喝酒过年。 作者|欧文 1月30日,白酒板块再现分化行情,皇台酒业却延续强势表现,...
深交所修订可持续发展报告编制指... 上证报中国证券网讯 据深交所1月30日消息,深交所发布实施《深圳证券交易所上市公司自律监管指南第3号...
面试餐饮|新手零经验,小红书开... 有没有餐饮人跟我一样?想靠小红书引流拓客,却卡在第一步:不知道怎么开店、怎么发笔记不踩雷,看着别人的...