本文共 3337 字,大约阅读时间需要 11 分钟。
#includeusing namespace std;struct SS { int x,y;};struct ltstr { bool operator() (SS a, SS b) { return a.x < b.x;} };int main() { set st; // st内的SS元素按x从小到大排序 …}
#includeusing namespace std;struct SS { int x,y; bool operator < (struct SS _s) const { if (x == _s.x) return y < _s.y; return x < _s.x; }};int main() { set st; // st内的SS元素按x从小到大排序 …}
multiset举例:
#include#include using namespace std;int main(void) { set set1; for (int i = 0; i < 10; ++i) set1.insert(i); for (set ::iterator p = set1.begin();p != set1.end();++p) cout << *p << ""; if (set1.insert(3).second)//把3插入到set1中//插入成功则set1.insert(3).second返回true,否则返回false//此例中,集中已经有3这个元素了,所以插入将失败 cout << "set insert success"; else cout << "set insert failed"; if (set1.find(3) != set1.end()) { // 查找元素3 cout << "find it.." << endl; } else { cout << "not find it.." << endl; }if (set1.find(300) != set1.end()) { // 查找元素100 cout << "find it.." << endl; } else { cout << "not find it.." << endl; } int a[] = { 4, 1, 1, 1, 1, 1, 0, 5, 1, 0}; multiset A; A.insert(set1.begin(), set1.end()); A.insert(a, a + 10); cout << endl; for (multiset ::iterator p = A.begin();p != A.end();++p) cout << *p << " "; cin.get(); return 0;}