set mySet;
/*插入元素*/
mySet.insert(5);
mySet.insert(4);
mySet.insert(3);
mySet.insert(2);
mySet.insert(2); //故意插入重复的元素
cout << "Elements in set" << endl;
for(const auto& elem : mySet) {
cout << elem << " ";
}
cout << endl;
/*查找元素*/
int searchValue = 5;
auto it = mySet.find(searchValue);
if(it != mySet.end()) {
cout << "find value in " << searchValue << " set" << endl;
} else {
cout << "not find value in " << searchValue << " set" << endl;
}
mySet.erase(5);
cout << "After delete 5 from set" << endl;
for(const auto& elem : mySet) {
cout << elem << " ";
}
cout << endl;
mySet.clear();
首先定义新的排序函数
struct MyCompare {
bool operator()(const int& a, const int& b) const {
return a > b;
}
}
声明新的集合
set newSet;