sort(v.begin(), v.end()) 替換手寫快排,意圖清晰、錯誤少。vector/list/deque/array 甚至指針。| 類別 | 代表算法 | 典型用途 |
|---|---|---|
| 查找 | find, find_if, binary_search, lower_bound, upper_bound, equal_range | 元素定位、區間查詢 |
| 排序 | sort, stable_sort, partial_sort, nth_element, is_sorted | 全序/部分序/中位數 |
| 修改 | transform, fill, fill_n, generate, generate_n, replace, replace_if | 批量賦值/映射/替換 |
| 複製/移動 | copy, copy_if, copy_n, move, move_backward, swap, iter_swap | 區間拷貝/轉移 |
| 刪除/去重 | remove, remove_if, remove_copy, unique, unique_copy | 配合 erase 刪除 |
| 排列組合 | next_permutation, prev_permutation, shuffle, rotate, reverse | 全排列/打亂/旋轉 |
| 分區/劃分 | partition, stable_partition, partition_point | 條件分組 |
| 集合運算 (有序區間) | merge, set_union, set_intersection, set_difference, set_symmetric_difference, includes | 有序區間合並/差集 |
| 堆操作 | make_heap, push_heap, pop_heap, sort_heap, is_heap | 優先隊列底層 |
| 數值/統計 | accumulate, inner_product, adjacent_difference, partial_sum, iota, count, count_if, minmax_element | 求和/統計/極值 |
vector v = {5, 2, 8, 2, 9, 1};
auto it = find(v.begin(), v.end(), 8); // 指向 8
auto it2 = find_if(v.begin(), v.end(), [](int x){ return x > 5; }); // 指向 8
long c = count(v.begin(), v.end(), 2); // 2
long c2 = count_if(v.begin(), v.end(), [](int x){ return x % 2 == 0; }); // 3
sort(v.begin(), v.end()); // 1 2 2 5 8 9
bool has = binary_search(v.begin(), v.end(), 5); // true
auto range = equal_range(v.begin(), v.end(), 2); // [指向第一個2, 指向5)
auto lb = lower_bound(v.begin(), v.end(), 4); // 指向 5 (第一個 ≥4)
auto ub = upper_bound(v.begin(), v.end(), 5); // 指向 8 (第一個 >5)
vector src = {1,2,3,4,5}, dst(5);
transform(src.begin(), src.end(), dst.begin(), [](int x){ return x*x; }); // 1 4 9 16 25
replace_if(dst.begin(), dst.end(), [](int x){ return x > 10; }, -1); // 1 4 9 -1 -1
vector even;
copy_if(src.begin(), src.end(), back_inserter(even), [](int x){ return x%2==0; }); // 2 4
vector v = {1,2,3,2,4,2,5};
v.erase(remove(v.begin(), v.end(), 2), v.end()); // 刪除所有 2 → 1 3 4 5
// C++20 直接: erase(v, 2);
vector v = {3,1,2,3,2,1};
sort(v.begin(), v.end()); // 1 1 2 2 3 3
v.erase(unique(v.begin(), v.end()), v.end()); // 1 2 3
vector a = {1,3,5}, b = {2,4,6}, res(6);
merge(a.begin(), a.end(), b.begin(), b.end(), res.begin()); // 1 2 3 4 5 6
vector h = {3,1,4,1,5,9};
make_heap(h.begin(), h.end()); // 大根堆: 9 5 4 1 1 3
h.push_back(7); push_heap(h.begin(), h.end()); // 插入 7
pop_heap(h.begin(), h.end()); int max = h.back(); h.pop_back(); // 取出 9
sort_heap(h.begin(), h.end()); // 升序排序,不再是堆
vector v = {1,2,3,4,5};
int sum = accumulate(v.begin(), v.end(), 0); // 15
int prod = accumulate(v.begin(), v.end(), 1, multiplies<>()); // 120
vector diff(5);
adjacent_difference(v.begin(), v.end(), diff.begin()); // 1 1 1 1 1
iota(v.begin(), v.end(), 100); // 填充 100 101 102 103 104
vector p = {1,2,3};
do { print(p); } while (next_permutation(p.begin(), p.end())); // 全排列
rotate(v.begin(), v.begin()+2, v.end()); // 左旋 2 位
remove/unique **不刪除元素**,只是覆蓋,**必須配合 erase**。binary_search/lower_bound 要求區間**已按相同準則排序**。set_union/intersection 輸出迭代器**不能指向輸入區間** (未定義行為)。const_iterator),Lambda 參數用引用 [](int& x) 才能修改原元素。std::begin/end (C++11) 支援數組/容器統一。C++20 直接用 std::ranges::sort(v) 更簡潔。<algorithm> 是 C++ 標準庫的皇冠上的明珠。熟練掌握 **查找、排序、修改、刪除、堆、數值** 五大核心族群,配合 Lambda 與 back_inserter/ostream_iterator,能將 90% 的容器處理邏輯縮減為幾行可讀、高效、標準的代碼。