艾辰做網(wǎng)站黑帽seo技巧
C++11 在標(biāo)準(zhǔn)庫(kù)中引入了一系列新的算法,這些新增的算法使我們的代碼寫起來(lái)更簡(jiǎn)潔方便。
下面是 C++11 中新增加的一些重要算法的簡(jiǎn)要描述和使用方法:
1、非修改序列操作
std::all_of
:檢查范圍內(nèi)的所有元素是否都滿足指定的謂詞。std::any_of
:檢查范圍內(nèi)是否存在滿足指定謂詞的元素。std::none_of
:檢查范圍內(nèi)是否沒(méi)有元素滿足指定的謂詞。std::find_if_not
:在范圍內(nèi)查找不滿足指定謂詞的第一個(gè)元素,這個(gè)算法和已經(jīng)存在的std::find_if
是相反的。
代碼示例
以下的示例代碼用來(lái)演示使用上面這些算法檢查容器中元素的屬性。
#include <iostream>
#include <vector>
#include <algorithm>int main() {// 初始化一個(gè)整數(shù)向量std::vector<int> numbers = {2, 4, 6, 8, 10, 12};// 檢查所有元素是否都是偶數(shù)bool allEven = std::all_of(numbers.begin(), numbers.end(), [](int x) {return x % 2 == 0;});std::cout << "All elements are even: " << std::boolalpha << allEven << std::endl;// 檢查是否存在大于10的元素bool anyGreaterThanTen = std::any_of(numbers.begin(), numbers.end(), [](int x) {return x > 10;});std::cout << "Any element greater than 10: " << anyGreaterThanTen << std::endl;// 檢查是否沒(méi)有元素小于0bool noneLessThanZero = std::none_of(numbers.begin(), numbers.end(), [](int x) {return x < 0;});std::cout << "No elements less than 0: " << noneLessThanZero << std::endl;// 查找第一個(gè)不是偶數(shù)的元素auto it = std::find_if_not(numbers.begin(), numbers.end(), [](int x) {return x % 2 == 0;});if (it != numbers.end()) {std::cout << "First element not even: " << *it << std::endl;} else {std::cout << "All elements are even" << std::endl;}return 0;
}
輸出:
All elements are even: true
Any element greater than 10: true
No elements less than 0: true
All elements are even
2、修改序列操作
std::copy_if
:復(fù)制滿足指定條件的元素到另一個(gè)容器。std::move
:將元素從一個(gè)容器移動(dòng)到另一個(gè)容器(move),而非拷貝(copy)。std::move_backward
:類似std::move
,但是從范圍的末尾開(kāi)始操作,適合某些特定的容器操作。
代碼示例
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> numbers = {1, 6, 3, 8, 5, 7, 2, 9};std::vector<int> copied;std::vector<int> moved(8); // 預(yù)分配空間以便使用std::move_backward// 1. 使用std::copy_if復(fù)制所有大于5的元素std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(copied), [](int n) { return n > 5; });std::cout << "Copied elements: ";for (auto n : copied) std::cout << n << " ";std::cout << "\n";// 2. 使用std::move將numbers的元素移動(dòng)到另一個(gè)向量std::vector<int> movedNumbers(std::make_move_iterator(numbers.begin()), std::make_move_iterator(numbers.end()));std::cout << "Moved elements: ";for (auto n : movedNumbers) std::cout << n << " ";std::cout << "\nOriginal vector (now empty elements): ";for (auto n : numbers) std::cout << n << " "; // 注意:numbers的元素現(xiàn)在處于未定義狀態(tài)std::cout << "\n";// 3. 使用 std::move_backward,移動(dòng)前 6 個(gè)元素std::move_backward(movedNumbers.begin(), std::next(movedNumbers.begin(), 6), moved.end());std::cout << "Elements after move_backward: ";for (auto n : moved) std::cout << n << " ";std::cout << "\n";return 0;
}
輸出:
Copied elements: 6 8 7 9
Moved elements: 1 6 3 8 5 7 2 9
Original vector (now empty elements): 1 6 3 8 5 7 2 9
Elements after move_backward: 0 0 1 6 3 8 5 7
注意第 2 步操作,我們使用 make_move_iterator
創(chuàng)建兩個(gè) move_iterator,如果使用下面這個(gè)語(yǔ)句:
std::vector<int> copiedNumbers(numbers.begin(), numbers.end());
將不會(huì)使用移動(dòng)方式,而是將數(shù)值拷貝到 copiedNumbers。
3、排序和相關(guān)操作
std::is_partitioned
:檢查給定范圍是否被分割成兩個(gè)滿足特定條件的子序列。std::partition_copy
:根據(jù)謂詞將元素分割并復(fù)制到兩個(gè)不同的容器。std::partition_point
:找到分割序列的分界點(diǎn)。
代碼示例
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>int main() {std::vector<int> numbers = {1, 9, 3, 8, 5, 7, 2, 6};// 先對(duì)向量進(jìn)行分區(qū)std::partition(numbers.begin(), numbers.end(), [](int n) { return n > 5; });std::cout << "Partitioned: ";for(auto n : numbers) std::cout << n << " ";std::cout << std::endl;// 檢查是否已分區(qū)bool partitioned = std::is_partitioned(numbers.begin(), numbers.end(), [](int n) { return n > 5; });std::cout << "Is partitioned: " << std::boolalpha << partitioned << "\n";std::vector<int> less_than_6, greater_than_5;// 復(fù)制分區(qū)元素std::partition_copy(numbers.begin(), numbers.end(), std::back_inserter(greater_than_5), std::back_inserter(less_than_6),[](int n) { return n > 5; });std::cout << "Elements greater than 5: ";for (auto n : greater_than_5) std::cout << n << " ";std::cout << "\nElements not greater than 5: ";for (auto n : less_than_6) std::cout << n << " ";std::cout << "\n";// 查找分區(qū)點(diǎn)auto partitionPoint = std::partition_point(numbers.begin(), numbers.end(), [](int n) { return n > 5; });std::cout << "Partition point at: " << *partitionPoint << "\n";return 0;
}
輸出:
Partitioned: 6 9 7 8 5 3 2 1
Is partitioned: true
Elements greater than 5: 6 9 7 8
Elements not greater than 5: 5 3 2 1
Partition point at: 5
4、數(shù)值操作
std::iota
:在給定范圍內(nèi)填充遞增序列,從而生成有序序列。
代碼示例
std::iota
定義在<numeric>
頭文件中。
#include <iostream>
#include <vector>
#include <numeric> // 包含 std::iotaint main() {std::vector<int> numbers(10); // 創(chuàng)建一個(gè)大小為10的vector// 使用std::iota填充numbers,使其元素從0開(kāi)始遞增std::iota(numbers.begin(), numbers.end(), 0);// 輸出填充后的vectorstd::cout << "The vector contains: ";for(int n : numbers) {std::cout << n << " ";}std::cout << std::endl;// 用std::iota生成另一個(gè)序列,此時(shí)起始值為10std::iota(numbers.begin(), numbers.end(), 10);// 輸出新的序列std::cout << "After reassigning, the vector contains: ";for(int n : numbers) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
輸出:
The vector contains: 0 1 2 3 4 5 6 7 8 9
After reassigning, the vector contains: 10 11 12 13 14 15 16 17 18 19
5、堆操作
std::is_heap
:檢查給定范圍是否形成一個(gè)堆。std::is_heap_until
:找到給定范圍中不滿足堆性質(zhì)的第一個(gè)位置。std::make_heap
、std::push_heap
、std::pop_heap
、std::sort_heap
:雖然這些函數(shù)在 C++11 之前就存在,但 C++11 對(duì)它們進(jìn)行了優(yōu)化和改進(jìn),提高了與新特性的兼容性。
#include <iostream>
#include <vector>
#include <algorithm>int main() {// 初始化一個(gè)未排序的整數(shù)向量std::vector<int> numbers = {4, 1, 3, 5, 2, 9, 7, 8, 6};// 使用std::make_heap將向量轉(zhuǎn)換為最大堆std::make_heap(numbers.begin(), numbers.end());std::cout << "After make_heap, numbers: ";for(int n : numbers) {std::cout << n << " ";}std::cout << std::endl;// 檢查是否為堆bool isHeap = std::is_heap(numbers.begin(), numbers.end());std::cout << "Is the vector a heap? " << std::boolalpha << isHeap << std::endl;// 向堆中添加新元素numbers.push_back(10);std::push_heap(numbers.begin(), numbers.end()); // 重新調(diào)整為堆std::cout << "After push_heap, numbers: ";for(int n : numbers) {std::cout << n << " ";}std::cout << std::endl;// 從堆中移除根元素std::pop_heap(numbers.begin(), numbers.end()); // 將最大元素移至末尾numbers.pop_back(); // 實(shí)際移除元素std::cout << "After pop_heap, numbers: ";for(int n : numbers) {std::cout << n << " ";}std::cout << std::endl;// 對(duì)堆進(jìn)行排序std::sort_heap(numbers.begin(), numbers.end());std::cout << "After sort_heap, numbers: ";for(int n : numbers) {std::cout << n << " ";}std::cout << std::endl;// 使用std::is_heap_until找到不是堆的第一個(gè)位置auto heapEnd = std::is_heap_until(numbers.begin(), numbers.end());std::cout << "The range is a heap until element: ";std::cout << (heapEnd - numbers.begin()) << std::endl;return 0;
}
輸出:
After make_heap, numbers: 9 8 7 6 2 3 4 5 1
Is the vector a heap? true
After push_heap, numbers: 10 9 7 6 8 3 4 5 1 2
After pop_heap, numbers: 9 8 7 6 2 3 4 5 1
After sort_heap, numbers: 1 2 3 4 5 6 7 8 9
The range is a heap until element: 1
6、最小/最大操作
std::minmax
:同時(shí)返回給定值中的最小值和最大值。std::minmax_element
:返回給定范圍中的最小元素和最大元素的迭代器。
代碼示例
#include <iostream>
#include <algorithm>
#include <vector>int main() {// 使用std::minmax對(duì)一組值進(jìn)行操作auto result = std::minmax({1, 3, 5, 7, 9, 2, 4, 6, 8, 0});std::cout << "The min value is: " << result.first << std::endl;std::cout << "The max value is: " << result.second << std::endl;// 使用std::minmax_element對(duì)容器中的元素進(jìn)行操作std::vector<int> numbers = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};auto minmaxPair = std::minmax_element(numbers.begin(), numbers.end());if (minmaxPair.first != numbers.end() && minmaxPair.second != numbers.end()) {std::cout << "The smallest element in the vector is: " << *minmaxPair.first << std::endl;std::cout << "The largest element in the vector is: " << *minmaxPair.second << std::endl;}return 0;
}
輸出將顯示:
The min value is: 0
The max value is: 9
The smallest element in the vector is: 0
The largest element in the vector is: 9
總結(jié)
這些新增的算法增強(qiáng)了 C++ 的標(biāo)準(zhǔn)庫(kù),為開(kāi)發(fā)者提供了更多的工具來(lái)編寫高效和簡(jiǎn)潔的代碼。
通過(guò)利用這些算法,可以減少手寫的代碼量,同時(shí)也可以保證代碼的性能和可讀性。