map:
1. mp1, mp2, 可以mp2 = mp1;
2. mp.find(key) 和 mp.erase(key) 复杂度都是log n
3. map与unordered_map:
map内部是一个红黑树,是有序且稳定的,内存占用低
unordered_map是一个hash表,在查找上时O1复杂度,因此执行查找的功能时更好
4 map自定义结构体时要重载< (用不着排序的话随便重载一下就行)
struct node {
int x;
int y;
bool operator<(const struct node & right)const{
return this->x < right.x;
}
};
vector
1. vec.erase(it) 用迭代器和while循环删除的时候不用it++!因为erase已经帮你+了
2. vector的初始化:
vector<int> vec(100); // 默认是0
vector<int> vec(100, -1);
vector<vector<int>> vec(row, vector<int>(col, 0)); // row*col 的全零数组
3. 在vector前端插入5个a
vec.insert(vec.begin(), 5, ‘a’);
string:
1. s.substr(pos, len);
deque
1. #include <deque>
2. 和queue相比,不同的操作:
push_back() | 在序列的尾部添加一个元素。 |
push_front() | 在序列的头部添加一个元素。 |
pop_back() | 移除容器尾部的元素。 |
pop_front() | 移除容器头部的元素。 |
priority_queue
刷题 | 优先队列_tuuzkiii_Tuu的博客-CSDN博客
set
自定义排序(重载运算符)
// 重载()
struct cmp{
bool operator()(int v1, int v2){
return v1 > v2;
}
};
// 重载 <
struct node{
string name
int cnt;
bool operator<(const node &rhs){
return cnt < rhs.cnt;
}
};
set<int, cmp> st1;
set<node> st2;
这里有个问题,说是stl重载运算符需要+const,但我编译器好像没问题??
“编译set的insert模板类成员函数时,会调用_Find_lower_bound函数,这是一个const 成员函数,只能调用bool operator()(const T& l,const T& r) const;”
ps类重载和结构体重载区别:C++类和结构体运算符重载区别_强尼爆紫的博客-CSDN博客_c++ 结构体重载比较
关于容器