实现小技巧
1.vector 收回内存
头文件:vector
vecotr<int>().swap(v); //收回v的内存
2.交换两个 vector 容器内的元素
vecotr<int>(v1).swap(v2); //交换v1和v2中的元素
3. 快读
ios::sync_with_stdio(0), cin.tie(0);
4.结构化绑定
C++17 往上才有
vector<pair<int, int>> e;
for (auto [u, v] : e) {
// 可以用 u 代替 .first,v 代替 .second
...
}
5. map 判断是否访问过元素 \(x\)
map<int, int> pos;
pos.find(x); // 若没访问过返回 pos.end();
pos.conut(x); // 返回 pos[x] 对应的数
// 以上两种适用于判断 x 是否被标记过,直接访问 pos[x] 使元素数变多
6. map 访问之前访问过的元素
map<int, int> pos;
for (auto [u, v] : pos) { //要访问映射和返回的两个值,结构化绑定
...
}
浙公网安备 33010602011771号