本文共 1267 字,大约阅读时间需要 4 分钟。
/*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 83: 1 4 84: 1 2 4 8not found.*/#include#include using namespace std;voidprint(set S) { cout << S.size() << ":"; for( set :: iterator it = S.begin(); it != S.end(); it++ ) { cout << " " << (*it); } cout << endl;}intmain() { set 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;}
转载地址:http://vpkxi.baihongyu.com/