一些基本操作
#include<set>
using namespace std;
int main(){
set <int> a{ 1, 2, 3, 4};
multiset <int> b;
set <int> c( a);
multiset <int> d( a.begin(), a.end());
}
#include<set>
#include<iostream>
using namespace std;
template <typename T>
void show(const T& a){
for(auto i = a.begin(); i != a.end(); i ++)
cout << *i << ' ';
cout << endl;
}
template <typename T>
struct SortDescending{
bool operator()(const T& a, const T& b) const{
return ( a > b);
}
};
int main(){
set <int> a;
a.insert(-1);
a.insert(4);
a.insert(1);
a.insert(2);
show( a);
set <int, SortDescending<int>> b;
b.insert(-1);
b.insert(4);
b.insert(1);
b.insert(2);
show( b);
multiset <int> c{-1, 4, 1, 2};
multiset <int> d( c);
d.insert(4);
show( d);
cout << "number of 4 is " << d.count( 4) << endl;
}
#include<set>
#include<iostream>
using namespace std;
int main(){
set <int> a{ 2, 1, 4, 3, 5};
for(auto i = a.begin(); i != a.end(); i ++)
cout << *i << ' ';
cout << endl;
auto p = a.find( 2);
if( p != a.end()) cout << *p << " is found" << endl;
else cout << "not found" << endl;
auto m = a.find( 10);
if( m != a.end()) cout << *m << " is found" << endl;
else cout << "not found" << endl;
}
#include<set>
#include<iostream>
using namespace std;
int main(){
multiset <int> a{ 1, 2, 3, 3, 3, 4, 5};
for(auto i = a.begin(); i != a.end(); i ++)
cout << *i << ' ';
cout << endl;
a.erase( 3);
for(auto i = a.begin(); i != a.end(); i ++)
cout << *i << ' ';
cout << endl;
auto p = ++a.begin();
a.erase( p);
for(auto i = a.begin(); i != a.end(); i ++)
cout << *i << ' ';
cout << endl;
a.erase( a.begin(), a.end());
cout << a.size() << endl;
}