集合用途:用于频繁而快速的搜索,存入集合的内容会进行排序,所以查找速度快,复杂度为对数
std::set:存储的元素唯一, 不重复,默认我从小到大的排序
std::multiset:存储的元素可重复,默认我从小到大的排序
std::unordered_set:散列集合,使用散列函数计算唯一索引,并根据索引决定将元素放入哪个bucket(桶),
#include <iostream>
#include <set>
#include <unordered_set>
using namespace std;
/*定义一个模板函数,目的是为了方便打印不同类型的值*/
template <typename T1, typename T2>
void DisplayContent(const T1& input, const T2& vName) {
int index = 0;
if (input.empty()) {
cout << " " << vName << " is empty, the size = " << input.size() << endl;
return;
}
for (auto Iterator = input.begin(); Iterator != input.end(); ++Iterator) {
index = std::distance(input.begin(), Iterator);
cout << " " << vName &
C++ STL 中的集合类提供了高效搜索的功能,元素会自动排序。std::set 存储唯一元素并按升序排列;std::multiset 允许元素重复;std::unordered_set 使用散列存储,元素不排序;std::unordered_multiset 同样允许元素重复但不排序。这些集合类各有特点,适用于不同的场景。
订阅专栏 解锁全文
404

被折叠的 条评论
为什么被折叠?



