STL set
作者:
AuroraYxh
,
2021-07-29 22:25:25
,
所有人可见
,
阅读 354
STL
set/unordered_set
//set
#include <iostream>
#include <cstdio>
#include <set>
#include <unordered_set>
using namespace std;
#define set unordered_set
int main()
{
set<int> S;
for (int i = 0; i < 5; i++)
S.insert(i);
//cout << S.begin() << endl;
for (set<int>::iterator i = S.begin(); i != S.end(); i++)
cout << *i << " ";
cout << endl;
cout <<"Size: "<< S.size() << endl;
cout << "max size: " << S.max_size() << endl;
//set<char> s;
//cout << s.max_size() << endl;
cout << *S.begin() << endl;
cout << *(--S.end()) << endl;
cout << "--------------------" << endl;
S.clear();
cout << S.size() << endl;
//S.insert(1);
if (S.empty()) cout << "empty" << endl;
else cout << "No" << endl;
cout << "--------------------" << endl;
S.insert(1);
S.insert(2);
S.insert(2);//set中元素不能重复
for (set<int>::iterator i = S.begin(); i != S.end(); i++)
cout << *i << " ";
cout << endl;
cout << S.count(1) << " " << S.count(2) << " " << S.count(0) << endl;
cout << "--------------------" << endl;
S.clear();
int a[] = { 1,2,3,4 };
set<int> s(a, a + 3);
set<int>::iterator iter;
if ((iter = s.find(2)) != s.end())
cout << *iter << endl;
if (!(s.find(5) != s.end()))
cout << "No value" << endl;
return 0;
}
/*
0 1 2 3 4
Size: 5
max size: 357913941
0
4
--------------------
0
empty
--------------------
1 2
1 1 0
--------------------
2
No value
*/