在本章中,我们将学习:
1. unordered_multiset简介。
2. unordered_multiset声明。
3. unordered_multiset成员函数获取Capacity。
4. unordered_multiset成员函数迭代元素
5. unordered_multiset成员函数,用于元素查找
6. unordered_multiset成员函数获取存储桶
7. unordered_multiset成员函数,用于修改元素
8.哈希策略的unordered_multiset成员函数
1. unordered_multiset简介。
unordered_multiset不按特定顺序存储元素。因此,它将允许基于单个元素的值快速检索它们。
您可以具有多个相同值的键。除了这个区别之外,所有的工作都类似于unordered_set。
元素的值同时是其键,可以唯一地标识它。
键是不可变的,因此无法对其进行修改,但可以将其插入和删除。
在内部,根据其哈希值将它们组织为存储桶,以允许通过其值直接快速访问各个元素。
使用unordered_set时要包含的头文件如下:
#include<unordered_set>
2. unordered_multiset声明。
std::unordered_multiset<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"};
3. unordered_multiset成员函数获取Capacity。
以下是为获取unordered_Set提供的成员函数:
空的 :它将测试容器是否为空
尺寸 :将返回容器大小
max_size :将返回最大大小
#include <iostream> #include <unordered_set> #include <algorithm> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com using namespace std; int main () { std::unordered_multiset<std::string> myset; if(myset.empty()) { std::cout << "myset is 空的. Size is = " << myset.size() << std::endl; } myset = {"Green","Yellow","Black"}; std::cout << "After 插入 尺寸: = " << myset.size() << std::endl; myset.insert ("White"); myset.insert ("White"); std::cout << "After 上 e more 插入 尺寸: = " << myset.size() << std::endl; myset.erase ("White"); std::cout << "After 抹去 尺寸: = " << myset.size() << std::endl; return 0; }
输出:
myset is 空的. Size is = 0 After 插入 尺寸: = 3 After 上 e more 插入 尺寸: = 5 After 抹去 尺寸: = 3
4. unordered_multiset成员函数迭代元素
以下是迭代元素的成员函数:
开始 :它将使迭代器返回到开始
结束 :它将返回迭代器结束
开始 :它将const_iterator返回到开头
结束 :它将返回const_iterator结束
#include <iostream> #include <unordered_set> #include <algorithm> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com using namespace std; int main () { std::unordered_multiset<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"}; cout<<"Printing by using 开始() and 结束()"<<endl; for(auto it = myset.begin(); it != myset.end(); it++) { cout<<*it<<endl; } cout<<"\nPrinting by using 开始() and 结束()"<<endl; for(auto it = myset.cbegin(); it != myset.cend(); it++) { cout<<*it<<endl; } return 0; }
输出:
Printing by using 开始() and 结束() Grey Black Black Yellow Yellow Green Brown Blue White Red Printing by using 开始() and 结束() Grey Black Black Yellow Yellow Green Brown Blue White Red
5. unordered_multiset成员函数,用于元素查找
以下是元素查找的成员函数:
找 :它将迭代器添加到元素
计数 :它将计算具有特定键的元素
等距 :它将获得具有特定键的元素范围
#include <iostream> #include <unordered_set> #include <algorithm> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com using namespace std; int main () { std::unordered_multiset<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"}; cout<<"Below shows the usage of 计数()"<<endl; for (auto& x: {"Yellow","violet","White","apple"}) { if (myset.count(x)>0) std::cout << "myset has " << x << std::endl; else std::cout << "myset has no " << x << std::endl; } cout<<"\nDemonstrate use of 找()"<<endl; std::unordered_set<std::string>::const_iterator got = myset.find ("Green"); if ( got == myset.end() ) std::cout << "not found in myset"; else std::cout << *got << " is in myset"; return 0; }
输出:
Below shows the usage of 计数() myset has Yellow myset has no violet myset has White myset has no apple Demonstrate use of 找() Green is in myset
6. unordered_multiset成员函数获取存储桶
以下是存储桶查找的成员函数:
桶_count :将返回存储桶数
max_bucket_count :将返回最大存储桶数
桶_size :将返回铲斗尺寸
桶 :定位元素’s 桶
#include <iostream> #include <unordered_set> #include <algorithm> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com using namespace std; int main () { std::unordered_multiset<std::string> myset = {"Red","Blue","Green","Yellow","Black","White","Grey","Brown", "Black", "Yellow"}; unsigned n = myset.bucket_count(); std::cout << "myset has " << n << " 桶s.\n"; for (unsigned i=0; i<n; ++i) { std::cout << "桶 #" << i << " 尺寸 is " << myset.bucket_size(i)<< " . It contains: "; for (auto it = myset.begin(i); it!=myset.end(i); ++it) std::cout << " " << *it; std::cout << "\n"; } cout<<"\nGetting element 桶:"<<endl; //get the element 桶 for (const std::string& x: myset) { std::cout << x << " is in 桶 #" << myset.bucket(x) << std::endl; } return 0; }
输出:
myset has 11 桶s. bucket #0 尺寸 is 2 . It contains: White Red bucket #1 尺寸 is 0 . It contains: bucket #2 尺寸 is 5 . It contains: Grey Black Black Yellow Yellow bucket #3 尺寸 is 1 . It contains: Green bucket #4 尺寸 is 0 . It contains: bucket #5 尺寸 is 0 . It contains: bucket #6 尺寸 is 0 . It contains: bucket #7 尺寸 is 0 . It contains: bucket #8 尺寸 is 2 . It contains: Brown Blue bucket #9 尺寸 is 0 . It contains: bucket #10 尺寸 is 0 . It contains: Getting element 桶: Grey is in 桶 #2 Black is in 桶 #2 Black is in 桶 #2 Yellow is in 桶 #2 Yellow is in 桶 #2 Green is in 桶 #3 Brown is in 桶 #8 Blue is in 桶 #8 White is in 桶 #0 Red is in 桶 #0
7. unordered_multiset成员函数,用于修改元素
以下是用于修改元素的成员函数:
插入 :插入元素
抹去 :擦除元素
明确 :清除内容(公共成员函数)
交换 :交换内容
#include <iostream> #include <unordered_set> #include <algorithm> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com using namespace std; int main () { std::unordered_multiset<std::string> myset; if(myset.empty()) { std::cout << "myset is 空的. Size is = " << myset.size() << std::endl; } myset = {"Green","Yellow","Black"}; std::cout << "After 插入 尺寸: = " << myset.size() << std::endl; myset.insert ("White"); myset.insert ("White"); std::cout << "After another 插入 尺寸: = " << myset.size() << std::endl; myset.erase ("White"); std::cout << "After 抹去 尺寸: = " << myset.size() << std::endl; myset.clear(); std::cout << "After 明确 尺寸: = " << myset.size() << std::endl; return 0; }
输出:
myset is 空的. Size is = 0 After 插入 尺寸: = 3 After another 插入 尺寸: = 5 After 抹去 尺寸: = 3 After 明确 尺寸: = 0
8.哈希策略的unordered_multiset成员函数
以下是哈希策略的成员函数:
load_factor :它将返回负载系数[load_factor = 尺寸 / 桶_count]
max_load_factor :将获取或设置最大负载系数
重新整理 :设置桶数
保留 :它将要求更改容量
该网站上可用的教程列表:
C编程20+章 | C ++编程80+章 |
100多个编码问题 | 数据结构和算法85+章 |
系统设计20+章 | Shell脚本编写12章 |
4g LTE 60+章节 | 最常见的编码问题 |
5G NR 50+章 | Linux系统编程20+章 |