STL set的基本用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <iostream> #include <set> using namespace std; int main() { /*set函数成员与map差不多,例如 begin() //迭代器 end() //迭代器 clear() //删除set容器中的所有的元素 empty() //判断set容器是否为空 max_size() //返回set容器可能包含的元素最大个数 size() //返回当前set容器中的元素个数 rbegin //逆迭代器 rend() //逆迭代器 */ set<int> s; //自动排序 set<int> :: iterator it; int number; int i; for( i=0; i<5; i++ ){ scanf("%d", &number ); s.insert( number ); } /*it = rs.begin(); it != rs.end(); it++ 反向迭代器写法*/ for( it = s.begin(); it != s.end(); it++ ){ cout<<*it<<' '; } cout<<endl; it = s.find(6); //查找键值为6的元素 if(it != s.end()) cout << *it << endl; /*判断一个数是否在集合中*/ for( i = 0; i < 5; i++) { scanf("%d", &number); if( !s.count(number) ) //不存在 printf("does not exist\n"); else printf("exist\n"); } return 0; } |