#include "pch.h" #include <iostream> #include <set> #include <string> using namespace std;
void printset(set<int> &s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) cout << *it << " "; }
void test01() { set<int> s1; s1.insert(5); s1.insert(1); s1.insert(9); s1.insert(7); s1.insert(3); printset(s1); if (s1.empty()) cout << "empty" << endl; else cout << "s1.size:" <<s1.size() << endl; s1.erase(s1.begin()); printset(s1); s1.erase(7); printset(s1); }
void test02() { set<int> s1; s1.insert(5); s1.insert(1); s1.insert(9); s1.insert(7); s1.insert(3); printset(s1); set<int>::iterator pos = s1.find(3); if (pos != s1.end()) { cout << "find it" << endl; } else cout << " not find it" << endl; for (auto it = pos; it != s1.end(); it++) cout << *it << endl; } void test03() { set<int> s1; s1.insert(5); s1.insert(1); s1.insert(9); s1.insert(7); s1.insert(3); int num = s1.count(2); cout << num << endl; } void test04() { set<int> s1; s1.insert(5); s1.insert(1); s1.insert(9); s1.insert(7); s1.insert(3); set<int>::iterator it = s1.lower_bound(3); if (it != s1.end()) cout << " find it"<< *it << endl; else cout << "not find it" << endl; set<int>::iterator it2 = s1.upper_bound(3); if (it2 != s1.end()) cout << " find it" << *it2<< endl; else cout << "not find it" << endl;
}
void test05() { set<int> s1; s1.insert(5); s1.insert(1); s1.insert(9); s1.insert(7); s1.insert(3); pair <set<int>::iterator, set<int>::iterator> ret = s1.equal_range(3); if (ret.first != s1.end()) cout << "找到equal_range的值" << *(ret.first) << endl; else cout << "未找到"; if (ret.second != s1.end()) cout << "找到equal_range的值" << *(ret.second) << endl; else cout << "未找到";
}
void test06() { set<int> s1; s1.insert(5); pair<set<int>::iterator, bool > ret = s1.insert(10); if (ret.second) cout << "插入成功" << endl; else cout << "插入失败" << endl;
ret = s1.insert(10); if (ret.second) cout << "第二次插入成功" << endl; else cout << "第二次插入失败" << endl; }
class MyCompare//仿函数 { public: bool operator()(int v1, int v2) { return v1 > v2; } };
void test07() { set<int,MyCompare> s1; s1.insert(5); s1.insert(1); s1.insert(9); s1.insert(7); s1.insert(3); for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++) { cout << *it << " "; }
}
class Person { public: Person(string name, int age) { this->name = name; this->age = age; } string name; int age;
};
class Mycompare { public: bool operator()(const Person & p1, const Person & p2) { if (p1.age > p2.age) { return true; } return false; } }; void test08() { set<Person,Mycompare> s1; Person p1 = { "葫芦娃",60 }; Person p2("大娃娃", 20); Person p3("二娃娃", 15); Person p4("三娃娃", 10); s1.insert(p1); s1.insert(p2); s1.insert(p3); s1.insert(p4); for (set<Person, Mycompare>::iterator it = s1.begin(); it != s1.end(); it++) cout << "姓名:" << (*it).name << " 年龄:" << (*it).age << endl;
}
void test09() { multiset<int> mul; mul.insert(1); mul.insert(1); for (auto c : mul) cout << c <<" "; } void mytest() { set<int> s; s.insert(1); s.insert(2); s.insert(3); s.insert(4); pair < set<int>::iterator, set<int>::iterator> res = s.equal_range(3);
} int main() { test09(); }
|