Files
Data-Structure/STL/STL-Set/set.cpp
e2hang 6ca2ae6356 STL
2025-08-13 14:53:22 +08:00

47 lines
973 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <string>
#include <set>
using namespace std;
int main(){
auto cmp = [](auto a, auto b){
return a > b;
};
set<int> a;
set<int> b = {1, 5, 4, 3, 3, 2};
set<int, decltype(cmp)> c(cmp);
a.insert(1);a.insert(5);a.insert(3);a.insert(2);
c.insert(1);c.insert(5);c.insert(3);c.insert(2);
a.emplace(6);
for(auto x : a){
cout << x << " ";
}
cout << endl;
for(auto x : c){
cout << x << " ";
}
cout << endl;
cout << a.count(3) << endl;
a.erase(6);
//没有.at()和a[i]树结构不能直接访问第i个可以it++
for(auto it = a.begin(); it != a.end(); it++){
cout << *it << " ";
}
//From "Smaller" To "Bigger", To compare big, we need "func:cmp"
cout << endl;
cout << *(c.find(5))<< endl;
//lower_bound : >= val ; upper_bound : > val ;
cout << *(c.lower_bound(2)) << " " << *(c.upper_bound(2)) << endl;
cout << *(a.lower_bound(2)) << " " <<*(a.upper_bound(2)) << endl;
return 0;
}
/*
COUT
1 2 3 5 6
5 3 2 1
1
1 2 3 5
5
2 1
2 3
*/