c++11 标准模板(STL)(std::unordered_multiset)(五)
迪丽瓦拉
2025-05-31 16:42:22
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() 访问。


容量

检查容器是否为空

std::unordered_multiset::empty

bool empty() const noexcept;

(C++11 起)
(C++20 前)

[[nodiscard]] bool empty() const noexcept;

(C++20 起)

 检查容器是否无元素,即是否 begin() == end() 。

参数

(无)

返回值

若容器为空则为 true ,否则为 false

复杂度

常数。

返回容纳的元素数

std::unordered_multiset::size

size_type size() const noexcept;

(C++11 起)

返回容器中的元素数,即 std::distance(begin(), end()) 。

参数

(无)

返回值

容器中的元素数量。

复杂度

常数。

返回可容纳的最大元素数

std::unordered_multiset::max_size

size_type max_size() const noexcept;

(C++11 起)

 返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。

参数

(无)

返回值

元素数量的最大值。

复杂度

常数。

注意

此值通常反映容器大小上的理论极限,至多为 std::numeric_limits::max() 。运行时,可用 RAM 总量可能会限制容器大小到小于 max_size() 的值。

调用示例

#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;};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;//检查容器是否无元素,即是否 begin() == end() 。std::unordered_multiset unordered_multiset1;std::cout << "unordered_multiset1 is empty " << unordered_multiset1.empty() << std::endl;unordered_multiset1 = {generate(), generate(), generate(), generate(), generate(), generate()};;std::cout << "unordered_multiset1:   ";std::copy(unordered_multiset1.begin(), unordered_multiset1.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl;std::cout << "unordered_multiset1 is empty " << unordered_multiset1.empty() << std::endl;std::cout << std::endl;std::unordered_multiset unordered_multiset2;for (size_t index = 0; index < 6; index++){unordered_multiset2.emplace(std::rand() % 10 + 100, std::rand() % 10 + 100);//返回容器中的元素数,即 std::distance(begin(), end()) 。std::cout << "unordered_multiset2 size:  " << unordered_multiset2.size() << std::endl;std::copy(unordered_multiset2.begin(), unordered_multiset2.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl;}std::cout << std::endl;//返回根据系统或库实现限制的容器可保有的元素最大数量,//即对于最大容器的 std::distance(begin(), end()) 。std::unordered_multiset unordered_multiset_c;std::cout << "unordered_multiset max_size:      " << unordered_multiset_c.max_size() << std::endl;std::unordered_multiset unordered_multiset_i;std::cout << "unordered_multiset max_size:       " << unordered_multiset_i.max_size() << std::endl;std::unordered_multiset unordered_multiset_ui8;std::cout << "unordered_multiset max_size:   " << unordered_multiset_ui8.max_size() << std::endl;std::unordered_multiset unordered_multiset_ui16;std::cout << "unordered_multiset max_size:  " << unordered_multiset_ui16.max_size() << std::endl;std::unordered_multiset unordered_multiset_ui32;std::cout << "unordered_multiset max_size:  " << unordered_multiset_ui32.max_size() << std::endl;std::unordered_multiset unordered_multiset_ui64;std::cout << "unordered_multiset max_size:  " << unordered_multiset_ui64.max_size() << std::endl;std::unordered_multiset unordered_multiset_s;std::cout << "unordered_multiset max_size:     " << unordered_multiset_s.max_size() << std::endl;std::unordered_multiset unordered_multiset_d;std::cout << "unordered_multiset max_size:    " << unordered_multiset_d.max_size() << std::endl;std::unordered_multiset unordered_multiset_f;std::cout << "unordered_multiset max_size:     " << unordered_multiset_f.max_size() << std::endl;std::unordered_multiset unordered_multiset_l;std::cout << "unordered_multiset max_size:      " << unordered_multiset_l.max_size() << std::endl;std::unordered_multiset unordered_multiset_ll;std::cout << "unordered_multiset max_size: " << unordered_multiset_ll.max_size() << std::endl;std::unordered_multiset unordered_multiset_str;std::cout << "unordered_multiset max_size:    " << unordered_multiset_str.max_size() << std::endl;std::unordered_multiset unordered_multiset_Cell;std::cout << "unordered_multiset max_size:      " << unordered_multiset_Cell.max_size() << std::endl;return 0;
}

输出

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...