STL 算法分類:
類別 | 常見算法 | 作用 |
---|---|---|
排序 | sort 、stable_sort 、partial_sort 、nth_element 等 | 排序 |
搜索 | find 、find_if 、count 、count_if 、binary_search 等 | 查找元素 |
修改 | copy 、replace 、replace_if 、swap 、fill 等 | 修改容器內容 |
刪除 | remove 、remove_if 、unique 等 | 刪除元素 |
歸約 | for_each 、accumulate 等 | 處理數據 |
合并 | merge 、set_union 、set_intersection 等 | 處理有序序列 |
排列組合 | next_permutation 、prev_permutation 等 | 生成排列 |
堆操作 | push_heap 、pop_heap 、make_heap 、sort_heap 等 | 處理堆 |
STL 修改算法
在 C++ 標準庫(STL)中,修改算法可以在容器中應用特定的操作,如修改元素的值、替換值、插入新元素等。
修改算法通常返回容器的迭代器,指示修改的位置或結果。
算法名稱 | 功能描述 | 時間復雜度 | 空間復雜度 | 適用場景 |
---|---|---|---|---|
fill | 用指定的值填充容器中的所有元素 | O(n) | O(1) | 填充容器的所有元素 |
fill_n | 用指定的值填充容器中的部分元素 | O(n) | O(1) | 填充容器的部分元素 |
copy | 將一個范圍內的元素復制到另一個范圍中 | O(n) | O(1) | 將容器中的元素復制到另一個容器 |
swap | 交換兩個元素的值 | O(1) | O(1) | 交換兩個元素或兩個容器的內容 |
replace | 替換容器中所有指定值的元素 | O(n) | O(1) | 替換容器中的指定值 |
replace_if | 替換容器中所有滿足條件的元素 | O(n) | O(1) | 根據條件替換容器中的元素 |
transform | 對容器中每個元素應用操作并存儲結果 | O(n) | O(1) | 對容器中每個元素進行變換 |
rotate | 將給定范圍內的元素旋轉指定次數 | O(n) | O(1) | 容器中元素的旋轉操作 |
(1)fill
- 功能:用指定的值填充容器中的所有元素。
- 時間復雜度:O(n),其中
n
是容器的元素數量。 - 空間復雜度:O(1),原地操作。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>int main() {vector<int> vec(5, 0); // 初始化為5個0fill(vec.begin(), vec.end(), 10); // 將所有元素修改為10for (int x : vec){cout << x << " "; // 輸出:10 10 10 10 10}cout << endl;system("pause");return 0;
}
注意:
fill
適用于,當需要用相同的值填充整個容器時。
(2)fill_n
- 功能:從指定位置開始填充指定數量的元素為給定值。
- 時間復雜度:O(n),其中
n
是填充的元素數量。 - 空間復雜度:O(1),原地操作。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>int main() {vector<int> vec(5, 0);fill_n(vec.begin(), 3, 10); // 將前3個元素填充為10for (int x : vec){cout << x << " "; // 輸出:10 10 10 0 0}cout << endl;system("pause");return 0;
}
注意:
fill_n
適用于,當需要填充容器的部分元素時。
(3)copy
- 功能:將一個范圍內的元素復制到另一個范圍中。
- 時間復雜度:O(n),其中
n
是源范圍中的元素數量。 - 空間復雜度:O(1),原地操作。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>int main() {vector<int> src = { 1, 2, 3, 4, 5 };vector<int> dest(5);// 將 src 中的元素復制到 dest 中copy(src.begin(), src.end(), dest.begin());cout << "復制的vector: ";for (int val : dest) {cout << val << " ";}cout << endl;system("pause");return 0;
}
注意:
copy
用于將一個容器中的元素復制到另一個容器,或從一個容器復制到數組等。
(4)swap
- 功能:交換兩個變量或容器中的元素。
- 時間復雜度:O(1),它直接交換兩個元素,不涉及其他復雜操作。
- 空間復雜度:O(1),原地操作。
示例:
#include <iostream>
using namespace std;
#include <algorithm>int main() {int a = 5, b = 10;// 交換 a 和 b 的值swap(a, b);cout << "a = " << a << ", b = " << b << endl;system("pause");return 0;
}
注意:
swap
適用于,當需要交換兩個元素或兩個容器的內容時。
(5)replace
- 功能:將容器中所有等于指定值的元素替換為新值。
- 時間復雜度:O(n),其中
n
是容器的元素數量。 - 空間復雜度:O(1),原地操作。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>int main() {vector<int> vec = { 1, 2, 3, 2, 5 };replace(vec.begin(), vec.end(), 2, 10); // 將所有2替換為10for (int x : vec){cout << x << " "; // 輸出:1 10 3 10 5}cout << endl;system("pause");return 0;
}
注意:
replace
適用于,當需要將容器中的指定值替換為其他值時。
(6)replace_if
- 功能:將容器中所有滿足條件的元素替換為新值。
- 時間復雜度:O(n),其中
n
是容器的元素數量。 - 空間復雜度:O(1),原地操作。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>bool is_odd(int n)
{return n % 2 != 0;
}int main() {vector<int> vec = { 1, 2, 3, 4, 5 };replace_if(vec.begin(), vec.end(), is_odd, 10); // 將所有奇數替換為10for (int x : vec){cout << x << " "; // 輸出:10 2 10 4 10}cout << endl;system("pause");return 0;
}
注意:
replace_if
適用于,當需要根據某種條件替換容器中元素時。
(7)transform
- 功能:對容器中的每個元素應用指定的操作,并將結果存儲到目標容器中。
- 時間復雜度:O(n),其中
n
是容器的元素數量。 - 空間復雜度:O(1)。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>int square(int n)
{return n * n;
}int main() {vector<int> vec = { 1, 2, 3, 4, 5 };vector<int> result(vec.size());transform(vec.begin(), vec.end(), result.begin(), square);for (int x : result){cout << x << " "; // 輸出:1 4 9 16 25}cout << endl;system("pause");return 0;
}
注意:
transform
適用于,當需要對容器中的每個元素進行某些變換時。
(8)rotate
- 功能:將范圍內的元素旋轉指定次數(左旋/循環左移)。
- 時間復雜度:O(n),其中
n
是范圍中的元素數量。 - 空間復雜度:O(1),原地操作。
示例:
#include <iostream>
#include <algorithm> // std::rotate
#include <vector>int main() {std::vector<int> vec = {1, 2, 3, 4, 5};// 將 vec 中的元素旋轉 2 次std::rotate(vec.begin(), vec.begin() + 2, vec.end());std::cout << "Rotated vector: ";for (int val : vec) {std::cout << val << " ";}std::cout << std::endl;return 0;
}
注意:
rotate
適用于,當需要將容器中的元素旋轉時。