set集合
- set一种容器,用于储存唯一的元素(不允许重复),并按照升序排序(<)
- 内部实现是红黑树
比较函数重置
greater
函数
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<int,greater<int>> mySet;
mySet.insert(25);
mySet.insert(17);
mySet.insert(39);
mySet.insert(42);
for(const auto& elem : mySet){
cout << elem <<" ";
}
cout << endl;
return 0;
}
- 自定义比较函数
#include<bits/stdc++.h>
using namespace std;
struct MyCompare{
bool operator()(const int& a,const int& b)const{
return a > b;
}
};
int main()
{
set<int,MyCompare> mySet;
mySet.insert(25);
mySet.insert(17);
mySet.insert(39);
mySet.insert(42);
for (const auto& elem : mySet){
cout << elem <<" ";
}
cout << endl;
return 0;
}
multiset多重集合*
- 与set不同允许重复元素
- 其中
erase()
会删除相同的所有元素 st.erase(st.find())
只会删除相同的一个元素
unordered_set无序集合
- 没有特定的顺序
- 实现基础是哈希表
- 即插入,删除和查找的时间复杂度都是常数时间,O(1)
代码示例
- set
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<int> myset;//声明set
//插入元素
myset.insert(5);
myset.insert(2);
myset.insert(8);
myset.insert(2);
cout << "Set elements: " ;
for(const auto& elem : myset){
cout << elem << " ";
}
cout << endl;
int searchValue = 5;//搜索5
auto it = myset.find(searchValue);
if(it != myset.end()){
cout << searchValue << " found in the set." << endl;
}else{
cout << searchValue << "not found in the set." << endl;
}
int removeValue = 2;//移除2
myset.erase(removeValue);
cout << "set elements after removal";
for(const auto& elem : myset){
cout << elem <<" ";
}cout <<endl;
myset.clear();//清空set
if(myset.empty())//判断是否为空
{
cout << " set is empty" << endl;
}else{
cout << "set is not empty" << endl;
}
return 0;
}
- multiset多重集合
#include<bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> myMultiset;
myMultiset.insert(5);
myMultiset.insert(2);
myMultiset.insert(8);
myMultiset.insert(2);
cout << "Multiset elements:";
for(const auto& elem : myMultiset){
cout << elem <<" ";
}cout << endl;
int searchValue = 5;
auto range = myMultiset.equal_range(searchValue);
if(range.first != range.second){
cout << searchValue <<" found in the multiset."<< endl;
}else{
cout << searchValue << " not found in the multiset."<< endl;
}
int removeValue = 2;
myMultiset.erase(removeValue);
cout << "Multiset elements after removal:";
for (const auto& elem : myMultiset){
cout << elem <<" ";
}cout << endl;
myMultiset.clear();
if(myMultiset.empty()){
cout << "Multiset is empty."<< endl;
}else{
cout << "Multiset is not empty." << endl;
}
return 0;
}
- unordered_set无序集合
#include<bits/stdc++.h>
using namespace std;
int main()
{
unordered_set<int> myUnorderedset;
myUnorderedset.insert(5);
myUnorderedset.insert(5);
myUnorderedset.insert(5);
myUnorderedset.insert(5);
cout << "Unordered set elements:" ;
for (const auto& elem : myUnorderedset){
cout << elem <<" ";
}cout << endl;
int searchValue = 5;
auto it = myUnorderedset.find(searchValue);
if(it != myUnorderedset.end()){
cout << searchValue << "found in the unordered set."<< endl;
}else{
cout << searchValue << "not found in the unordered set."<<endl;
}
int removeValue = 2;
myUnorderedset.erase(removeValue);
cout << "Unordered set elements after removal:";
for(const auto& elem : myUnorderedset){
cout << elem <<" ";
}cout << endl;
myUnorderedset.clear();
if(myUnorderedset.empty()){
cout << "Unordered set is empty."<< endl;
}else{
cout << "Unordered set is not empty."<< endl;
}
return 0;
}
标签:myUnorderedset,insert,set,cout,int,mySet
From: https://www.cnblogs.com/777CC/p/18050482