STL使用

STL使用

1
2
3
string 替换指定字符
replace(str.begin(), str.end(), '/', '.')
会将str字符串中所有的 '/' 替换成 '.'

二、最大堆、最小堆

priority_queue 默认是最大堆

1
2
priority_queue<int, vector<int>, less<int>> maxHeap;  // 最大堆 
priority_queue<int, vector<int>, greater<int>> minHeap; // 最小堆

也可以自定义比较函数

1
2
3
4
5
6
7
8
9
10
11
12
13
// 内置类型
struct cmp {
bool operator()(const int &a, const int &b) {
return a > b;
}
};
// 非内置类型
struct cmp {
bool operator()(const Node&a, const Node&b) {
return a.val > b.val;
}
};
priority_queue<int, vector<int>, cmp> minHeap;