set、multiset集合
A set is a data structure that maintains a collection of elements. The basic operations of sets are element insertion, search and removal.The benefit of the set structure is that it maintains the order of the elements.
vector 如果要排序,需要 sort 一下; set 就会默认是有序的
set<int> s;s.insert(3);s.insert(2);s.insert(5);cout << s.count(3) << "\n"; // 1cout << s.count(4) << "\n"; // 0s.erase(3);s.insert(4);cout << s.count(3) << "\n"; // 0cout << s.count(4) << "\n"; // 1
set<int> s = {2,5,6,8};cout << s.size() << "\n"; // 4for (auto x : s) { //C++11 用法cout << x << "\n";}
//set里面,每个元素只存一个set<int> s;s.insert(5);s.insert(5);s.insert(5);cout << s.count(5) << "\n"; // 1
//multiset里面,每个元素可以存多个multiset<int> s;s.insert(5);s.insert(5);s.insert(5);cout << s.count(5) << "\n"; // 3
//把5这个元素全删了s.erase(5);cout << s.count(5) << "\n"; // 0//只删掉一个5元素s.erase(s.find(5));cout << s.count(5) << "\n"; // 2
printf("最小值 %d\n", *st.begin());printf("最大值 %d\n", *(--st.end()));printf("最大值 %d\n", *st.rbegin());set<int>::iterator it;// 第一个大于等于it = st.lower_bound(10);if (it != st.end()) cout << *it << '\n';// 第一个大于it = st.upper_bound(10);if (it != st.end()) cout << *it << '\n';// 最后一个小于it = st.lower_bound(10);if (it != st.begin()){it--;cout << *it << '\n';}// 注意不要访问不存在的东西,也不要删除不存在的东西
std::set::lower_bound 与 std::lower_bound的效率问题
// 两者效率相差大的几乎是天壤之别。// 在CSP-J2021第二轮认证中,T4小熊的果篮// 这都是血泪史啊// 这样写就TLE,只能拿30ptsit = upper_bound(st[p].begin(), st[p].end(), now);// 这样写就可以ACit = st[p].upper_bound(now);


我倒是觉得下面那个说的很有道理,应该是set<>::iterator不支持随机访问,所以直接当作普通的序列进行二分的时候就不是O(logn)的复杂度了。所以,一定要使用std::set::lower_bound。
https://blog.csdn.net/CZWin32768/article/details/51752267


