stl & set
/*
set是根据元素值进行排序的集合,所插入的元素在集合中唯一,不存在重复元素。
______________________________________________________________________
size() 返回set中的元素数 O(1)
clear() 清空set O(n)
begin() 返回指向set开头的迭代器 O(1)
end() 返回指向set末尾的迭代器 O(1)
insert(key) 向set中插入元素key O(logn)
erase(key) 删除含有key的元素 O(logn)
find(key) 搜索与key一致的元素,并返回指向该元素的迭代器 O(logn)
没有与key一致的元素,则返回末尾end()
______________________________________________________________________
output:
4: 1 4 7 8
3: 1 4 8
4: 1 2 4 8
not found.
*/
#include <iostream>
#include <set>
using namespace std;
void
print(set <int> S) {
cout << S.size() << ":";
for( set <int> :: iterator it = S.begin(); it != S.end(); it++ ) {
cout << " " << (*it);
}
cout << endl;
}
int
main() {
set <int> S;
S.insert(8);
S.insert(1);
S.insert(7);
S.insert(4);
S.insert(8);
S.insert(4);
print(S); // 4: 1 4 7 8
S.erase(7);
print(S); // 3: 1 4 8
S.insert(2);
print(S); // 4: 1 2 4 8
if( S.end() == S.find(10) ) {
cout << "not found." << endl;
}
return 0;
}