algorithm
C++ 中的
不要花時間重造輪子了
以下是一些常用的 12 個函數和它們的簡要介紹!
std::sort : 用於對範圍內的元素進行排序
1
2
3
4
5
6
7
8
9
10
11
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {5, 2, 9, 1, 5, 6};
std::sort(vec.begin(), vec.end());
for(int n : vec)
std::cout << n << ' ';
return 0;
}
std::reverse: 用於反轉範圍內的元素順序
1
2
3
4
5
6
7
8
9
10
11
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::reverse(vec.begin(), vec.end());
for(int n : vec)
std::cout << n << ' ';
return 0;
}
std::find:用於在範圍內查找某個元素,返回指向該元素的迭代器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = std::find(vec.begin(), vec.end(), 3);
if(it != vec.end())
std::cout << "Found: " << *it << '\n';
else
std::cout << "Not found\n";
return 0;
}
std::accumulate: 用於對範圍內的元素進行累積求和。需要包含
1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <numeric>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << "Sum: " << sum << '\n';
return 0;
}
std::copy:用於將範圍內的元素複製到另一個範圍。
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec1 = {1, 2, 3, 4, 5};
std::vector<int> vec2(vec1.size());
std::copy(vec1.begin(), vec1.end(), vec2.begin());
for(int n : vec2)
std::cout << n << ' ';
return 0;
}
std::copy_if: 用於將符合條件的元素複製到另一個範圍。
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec1 = {1, 2, 3, 4, 5, 6};
std::vector<int> vec2;
std::copy_if(vec1.begin(), vec1.end(), std::back_inserter(vec2), [](int x) { return x % 2 == 0; });
for(int n : vec2)
std::cout << n << ' ';
return 0;
}
std::transform: 用於對範圍內的元素應用某個操作並將結果存儲在另一個範圍。
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec1 = {1, 2, 3, 4, 5};
std::vector<int> vec2(vec1.size());
std::transform(vec1.begin(), vec1.end(), vec2.begin(), [](int x) { return x * x; });
for(int n : vec2)
std::cout << n << ' ';
return 0;
}
std::all_of, std::any_of, std::none_of 用於檢查範圍內的元素是否全部、任何或沒有符合條件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
bool all_even = std::all_of(vec.begin(), vec.end(), [](int x) { return x % 2 == 0; });
bool any_even = std::any_of(vec.begin(), vec.end(), [](int x) { return x % 2 == 0; });
bool none_even = std::none_of(vec.begin(), vec.end(), [](int x) { return x % 2 == 0; });
std::cout << "All even: " << all_even << '\n';
std::cout << "Any even: " << any_even << '\n';
std::cout << "None even: " << none_even << '\n';
return 0;
}
std::unique: 用於移除範圍內的連續重複元素,返回新的末尾迭代器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 1, 2, 3, 3, 4, 5, 5, 6};
auto it = std::unique(vec.begin(), vec.end());
vec.erase(it, vec.end()); // 移除多餘的元素
for(int n : vec)
std::cout << n << ' ';
return 0;
}
std::lower_bound: 用於在已排序範圍內查找不小於給定值的第一個元素。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5, 6};
int value = 4;
auto it = std::lower_bound(vec.begin(), vec.end(), value);
if (it != vec.end())
std::cout << "Lower bound of " << value << " is " << *it << '\n';
else
std::cout << "Value not found\n";
return 0;
}
std::upper_bound:用於在已排序範圍內查找大於給定值的第一個元素。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5, 6};
int value = 4;
auto it = std::upper_bound(vec.begin(), vec.end(), value);
if (it != vec.end())
std::cout << "Upper bound of " << value << " is " << *it << '\n';
else
std::cout << "Value not found\n";
return 0;
}
std::partition: 用於根據給定的謂詞條件重新排列範圍內的元素,使得符合條件的元素排在前面,不符合條件的排在後面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5, 6};
auto it = std::partition(vec.begin(), vec.end(), [](int x) { return x % 2 == 0; });
std::cout << "Partitioned vector: ";
for(int n : vec)
std::cout << n << ' ';
std::cout << '\n';
std::cout << "First element not fulfilling the condition: " << *it << '\n';
return 0;
}
整理得好辛苦啊!呼












