目录
1. 关于 \(\rm stl\) 的 \(\rm blogs\)~
2. \(\rm set\)
2.1. \(\mathtt{upper\_bound(),lower\_bound()}\)
- \(\mathtt{upper\_bound()}\):第一个大于 \(\rm val\) 的数。
- \(\mathtt{lower\_bound()}\):第一个大于等于 \(\rm val\) 的数。
需要注意的是如果没有符合条件的数就会返回 \(\mathtt{set.end()}\).
2.2. \(\mathtt{swap()}\)
\(\mathtt{swap()}\) 的定义:
/**
* @brief Swaps data with another %set.
* @param x A %set of the same element and allocator types.
*
* This exchanges the elements between two sets in constant time.
* (It is only swapping a pointer, an integer, and an instance of
* the @c Compare type (which itself is often stateless and empty), so it
* should be quite fast.)
* Note that the global std::swap() function is specialized such that
* std::swap(s1,s2) will feed to this function.
*/
用法是 \(\mathtt{set\_1.swap(set\_2)}\).
2.3. \(\mathtt{rbegin()}\) 与 \(\mathtt{rend()}\)
返回指向 \(\mathtt{set}\) 末尾的 反向 迭代器,所以取出时要这样写:
set <int> :: reverse_iterator it = s.rbegin();
另外注意 ++ it
此时变成了向前遍历。
2.4. \(\mathtt{count()}\)
复杂度为 \(\mathcal O(\log |S|)\).
2.5. \(\mathtt{erase()}\)
erase(position)
:均摊 线性。erase(val)
:\(\mathcal O(\log |S|)\).
3. \(\text{multiset}\)
3.1. \(\mathtt{erase()}\)
- \(\mathtt{erase(}\)值\(\mathtt )\):删除这个值的所有元素。
- \(\mathtt{erase(}\)迭代器\(\mathtt )\):删除这个迭代器指向的元素。
4. \(\text{map}\)
4.1. 查询
-
\(\mathtt{find()}\): 如果不存在这个下标,返回值为 \(\mathtt{map.end()}\)。
-
\(\mathtt{count()}\): 如果存在这个下标,返回值为 \(1\),否则为 \(0\)。
-
\(\mathtt{map[]}\): 如果存在这个下标,返回值为这个下标的值,否则为 \(0\)。
敲黑板:\(\mathtt{map[]}\) 在询问不存在的下标时,第一次会返回 \(0\),但之后会填个东西进下标,之后的查询无论使用哪一种操作都是错的!!!
4.2. 插入
- \(\mathtt{map[x]=y}\)。
- \(\mathtt{map.insert(make\_pair(x,y))}\)。
好像第二种要快一点,因为不用查找。
4.3. \(\mathtt{upper\_bound(),lower\_bound()}\)
- \(\mathtt{upper\_bound()}\):第一个大于 \(\rm val\) 的键值。
- \(\mathtt{lower\_bound()}\):第一个大于等于 \(\rm val\) 的键值。
返回迭代器为 it->first
为键值,it->second
为值。
5. \(\rm vector\)
5.1. \(\mathtt{push\_back()}\) 与 \(\mathtt{emplace\_back()}\)
5.2. \(\mathtt{swap()}\)
\(\rm vector\) 的 \(\mathtt{clear()}\) 函数并不提供内存释放,而只是清除元素。内存释放需要使用 \(\mathtt{swap()}\).