查找类
find(first, last, x)
找第一个等于 x 的元素,返回迭代器,找不到返回 last。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 7, 9};
auto it = find(v.begin(), v.end(), 5);
if (it != v.end()) cout << "找到了:" << *it << endl;
else cout << "没找到" << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 7, 9};
auto it = find(v.begin(), v.end(), 5);
if (it != v.end()) cout << "找到了:" << *it << endl;
else cout << "没找到" << endl;
return 0;
}find_if(first, last, 条件函数)
找第一个满足条件的元素。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 4, 7, 9};
auto it = find_if(v.begin(), v.end(),
[](int x){ return x % 2 == 0; });
if (it != v.end()) cout << "第一个偶数:" << *it << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 4, 7, 9};
// 找第一个偶数
auto it = find_if(v.begin(), v.end(),
[](int x){ return x % 2 == 0; });
if (it != v.end()) cout << "第一个偶数:" << *it << endl;
return 0;
}count(first, last, x)
统计 x 出现次数。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 3, 7, 3};
int n = count(v.begin(), v.end(), 3);
cout << "3出现次数:" << n << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 3, 7, 3};
int n = count(v.begin(), v.end(), 3);
cout << "3出现次数:" << n << endl; // 3
return 0;
}count_if(first, last, 条件)
按条件统计。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6};
int n = count_if(v.begin(), v.end(),
[](int x){ return x%2==0; });
cout << "偶数个数:" << n << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6};
// 统计偶数个数
int n = count_if(v.begin(), v.end(),
[](int x){ return x%2==0; });
cout << "偶数个数:" << n << endl; // 3
return 0;
}binary_search(first, last, x)
要求序列已排序!判断 x 是否存在,返回 bool。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 7, 9};
bool exists = binary_search(v.begin(), v.end(), 5);
cout << "5是否存在:" << (exists ? "是" : "否") << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 7, 9}; // 已排序
bool exists = binary_search(v.begin(), v.end(), 5);
cout << "5是否存在:" << (exists ? "是" : "否") << endl;
return 0;
}lower_bound / upper_bound(要求已排序)
lower_bound:第一个 >= x 的位置
upper_bound:第一个 > x 的位置
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 5, 7};
auto it1 = lower_bound(v.begin(), v.end(), 5);
auto it2 = upper_bound(v.begin(), v.end(), 5);
cout << "lower_bound指向:" << *it1 << endl;
cout << "5的个数:" << it2 - it1 << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 5, 7}; // 已排序
auto it1 = lower_bound(v.begin(), v.end(), 5); // 第一个 >=5
auto it2 = upper_bound(v.begin(), v.end(), 5); // 第一个 >5
cout << "lower_bound指向:" << *it1 << endl;
cout << "5的个数:" << it2 - it1 << endl; // 2
return 0;
}排序类
sort(first, last)
默认升序排序。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
sort(v.begin(), v.end());
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
sort(v.begin(), v.end());
for (int x : v) cout << x << " "; // 1 1 3 4 5
cout << endl;
return 0;
}sort(first, last, 比较函数)
自定义排序。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
sort(v.begin(), v.end(), greater<int>());
for (int x : v) cout << x << " ";
cout << endl;
vector<int> v2 = {-3, 1, -4, 2};
sort(v2.begin(), v2.end(), [](int a,int b){ return abs(a)<abs(b); });
for (int x : v2) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
// 降序排序
sort(v.begin(), v.end(), greater<int>());
for (int x : v) cout << x << " "; // 5 4 3 1 1
cout << endl;
// 自定义:按绝对值排序
vector<int> v2 = {-3, 1, -4, 2};
sort(v2.begin(), v2.end(), [](int a,int b){ return abs(a)<abs(b); });
for (int x : v2) cout << x << " "; // 1 2 -3 -4
cout << endl;
return 0;
}stable_sort
稳定排序,相等元素的相对顺序保持不变。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
stable_sort(v.begin(), v.end());
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
stable_sort(v.begin(), v.end());
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}partial_sort
只把前 N 个排好序,后面不管,比全排序快。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {5, 3, 1, 4, 2};
partial_sort(v.begin(), v.begin()+3, v.end());
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {5, 3, 1, 4, 2};
partial_sort(v.begin(), v.begin()+3, v.end()); // 只排前3个
for (int x : v) cout << x << " "; // 1 2 3 5 4(前3个有序)
cout << endl;
return 0;
}nth_element
第 N 个位置放正确元素(左边都小,右边都大),左右不一定有序。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {5, 3, 1, 4, 2};
nth_element(v.begin(), v.begin()+2, v.end());
cout << "第3小的元素:" << v[2] << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {5, 3, 1, 4, 2};
nth_element(v.begin(), v.begin()+2, v.end());
cout << "第3小的元素:" << v[2] << endl; // 3
return 0;
}reverse(first, last)
反转。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
reverse(v.begin(), v.end());
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
reverse(v.begin(), v.end()); // {1,2,3} → {3,2,1}
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}is_sorted(first, last)
判断是否已排序。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
if (is_sorted(v.begin(), v.end())) cout << "已排序" << endl;
else cout << "未排序" << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
if (is_sorted(v.begin(), v.end())) cout << "已排序" << endl;
else cout << "未排序" << endl;
return 0;
}修改类
copy(first, last, dest)
复制到从 dest 开始的位置。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> src={1,2,3}, dst(3);
copy(src.begin(), src.end(), dst.begin());
for (int x : dst) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> src={1,2,3}, dst(3);
copy(src.begin(), src.end(), dst.begin());
for (int x : dst) cout << x << " "; // 1 2 3
cout << endl;
return 0;
}fill(first, last, x)
范围内所有元素设为 x。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
fill(v.begin(), v.end(), 0);
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
fill(v.begin(), v.end(), 0); // 全部填0
for (int x : v) cout << x << " "; // 0 0 0 0 0
cout << endl;
return 0;
}replace(first, last, old, new)
所有等于 old 的元素换成 new。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 1, 3, 1};
replace(v.begin(), v.end(), 1, 99);
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 1, 3, 1};
replace(v.begin(), v.end(), 1, 99); // 所有1变成99
for (int x : v) cout << x << " "; // 99 2 99 3 99
cout << endl;
return 0;
}transform(first, last, dest, 函数)
对每个元素执行操作,结果存到 dest。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
transform(v.begin(), v.end(), v.begin(),
[](int x){ return x*2; });
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
// 每个元素乘2,结果存回v
transform(v.begin(), v.end(), v.begin(),
[](int x){ return x*2; });
for (int x : v) cout << x << " "; // 2 4 6
cout << endl;
return 0;
}remove / remove_if
⚠️ remove 不会真正删除,只是把要保留的移到前面,返回新末尾迭代器。
1. 要真正删除需配合 erase(删除-擦除惯用法): v.erase(remove(v.begin(), v.end(), 5), v.end()); [示例] #include <algorithm> #include <vector> #include <iostream> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5, 6}; // remove_if 删除所有偶数 v.erase(remove_if(v.begin(), v.end(), [](int x){ return x%2==0; }), v.end()); for (int x : v) cout << x << " "; // 1 3 5 cout << endl; return 0; } unique 同样不会真正删除,把重复元素移到后面。
2. 通常先排序再去重: sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); [示例] #include <algorithm> #include <vector> #include <iostream> using namespace std; int main() { vector<int> v={1,1,2,2,3}; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); // {1,2,3} for (int x : v) cout << x << " "; cout << endl; return 0; } swap(a, b) 交换两个变量或容器。
3. [示例] #include <algorithm> #include <vector> #include <iostream> using namespace std; int main() { int a=1, b=2; swap(a, b); // a=2, b=1 cout << "a=" << a << ", b=" << b << endl; vector<int> v1={1,2}, v2={3,4}; swap(v1, v2); // 交换两个vector return 0; }
最值类
min(a, b) / max(a, b) 两个数的最小/最大
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int m = min(3, 5);
int M = max(3, 5);
cout << "min=" << m << ", max=" << M << endl;
return 0;
}
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int m = min(3, 5); // m = 3
int M = max(3, 5); // M = 5
cout << "min=" << m << ", max=" << M << endl;
return 0;
}min_element / max_element 范围内最小/最大元素(返回迭代器)
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
auto it = min_element(v.begin(), v.end());
cout << "最小值:" << *it << endl;
auto it2 = max_element(v.begin(), v.end());
cout << "最大值:" << *it2 << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
auto it = min_element(v.begin(), v.end());
cout << "最小值:" << *it << endl; // 1
auto it2 = max_element(v.begin(), v.end());
cout << "最大值:" << *it2 << endl; // 5
return 0;
}minmax_element 同时找最小和最大(返回 pair)
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
auto p = minmax_element(v.begin(), v.end());
cout << "最小:" << *p.first << ",最大:" << *p.second << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5};
auto p = minmax_element(v.begin(), v.end());
cout << "最小:" << *p.first << ",最大:" << *p.second << endl;
return 0;
}其他常用
for_each(first, last, 函数)
对每个元素执行操作。范围 for 更直观,这个用得少了。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
for_each(v.begin(), v.end(), [](int x){ cout << x << " "; });
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
for_each(v.begin(), v.end(), [](int x){ cout << x << " "; });
cout << endl;
return 0;
}merge
合并两个有序序列为一个有序序列。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> a={1,3}, b={2,4}, c(4);
merge(a.begin(),a.end(), b.begin(),b.end(), c.begin());
for (int x : c) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> a={1,3}, b={2,4}, c(4);
merge(a.begin(),a.end(), b.begin(),b.end(), c.begin());
// c = {1,2,3,4}
for (int x : c) cout << x << " ";
cout << endl;
return 0;
}next_permutation
生成字典序下一个排列,常用于全排列枚举。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1,2,3};
do {
for (int x : v) cout << x << " ";
cout << endl;
} while (next_permutation(v.begin(), v.end()));
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1,2,3};
do {
for (int x : v) cout << x << " ";
cout << endl;
} while (next_permutation(v.begin(), v.end()));
return 0;
}shuffle / random_shuffle
随机打乱。
#include <algorithm>
#include <vector>
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
shuffle(v.begin(), v.end(), mt19937(time(0)));
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
shuffle(v.begin(), v.end(), mt19937(time(0)));
for (int x : v) cout << x << " ";
cout << endl;
return 0;
}集合操作(要求已排序)
set_union 并集 | set_intersection 交集 | set_difference 差集 | includes 包含
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> a={1,2,3}, b={2,3,4}, c(10);
auto it = set_union(a.begin(),a.end(), b.begin(),b.end(), c.begin());
for (auto p = c.begin(); p != it; ++p) cout << *p << " ";
cout << endl;
bool ok = includes(a.begin(),a.end(), b.begin(),b.end());
cout << "b包含于a:" << (ok ? "是" : "否") << endl;
return 0;
}
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> a={1,2,3}, b={2,3,4}, c(10);
// 并集
auto it = set_union(a.begin(),a.end(), b.begin(),b.end(), c.begin());
for (auto p = c.begin(); p != it; ++p) cout << *p << " ";
cout << endl; // 1 2 3 4
// 判断 b 是否包含于 a
bool ok = includes(a.begin(),a.end(), b.begin(),b.end());
cout << "b包含于a:" << (ok ? "是" : "否") << endl; // 否
return 0;
}