std::partial_sort用於對整個容器內的範圍進行排序。因此,如果我們要保持原始容器不變,而隻是將已排序的容器的sub-part複製到另一個容器中,則可以使用std::partial_sort_copy。
就像std::partial_sort一樣,partial_sort_copy()可以通過兩種方式使用,如下所示:
-
使用<:比較元素
用法:
Template RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); first: Input iterator to the first element in the container. last: Input iterator to the last element in the container. result_first: Random-Access iterator pointing to the initial position in the destination container. result_last: Random-Access iterator pointing to the final position in the destination container. 返回值:It returns an iterator pointing to the element that follows the last element written in the result sequence.
// C++ program to demonstrate the use of // std::partial_sort_copy #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 1, 1, 3, 10, 3, 3, 7, 7, 8 }, v1(3); vector<int>::iterator ip; // Using std::partial_sort_copy std::partial_sort_copy(v.begin(), v.end(), v1.begin(), v1.end()); // Displaying the vector after applying // std::partial_sort_copy for (ip = v1.begin(); ip != v1.end(); ++ip) { cout << *ip << " "; } return 0; }
輸出:
1 1 3
在這裏,由於我們聲明v1的大小為3,因此僅存儲了三個元素。
- 通過使用預定義函數進行比較:
用法:
Template RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); Here, first, last, result_first and result_last are the same as previous case. comp: Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object. 返回值:It returns an iterator pointing to the element that follows the last element written in the result sequence.
// C++ program to demonstrate the use of // std::partial_sort_copy #include <iostream> #include <algorithm> #include <vector> using namespace std; // Defining the BinaryFunction bool comp(int a, int b) { return (a < b); } int main() { vector<int> v = { 1, 1, 3, 10, 3, 3, 7, 7, 8 }, v1(7); vector<int>::iterator ip; // Using std::partial_sort_copy std::partial_sort_copy(v.begin(), v.end(), v1.begin(), v1.end(), comp); // Displaying the vector after applying // std::partial_sort_copy for (ip = v1.begin(); ip != v1.end(); ++ip) { cout << *ip << " "; } return 0; }
輸出:
1 1 3 3 3 7 7
Where to use it ?
- 複製排序範圍:因此,隻要我們希望原始容器在排序後保持不變並將partial_sort的結果存儲在另一個容器中,則可以使用它。
// C++ program to demonstrate the use of // std::partial_sort_copy #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 100, 45, 78, 23, 220 }, v1(5); vector<int>::iterator ip; // Using std::partial_sort_copy std::partial_sort_copy(v.begin(), v.end(), v1.begin(), v1.end()); // Displaying the vectors after applying // std::partial_sort_copy cout << "v = "; for (ip = v.begin(); ip != v.end(); ++ip) { cout << *ip << " "; } cout << "\nv1 = "; for (ip = v1.begin(); ip != v1.end(); ++ip) { cout << *ip << " "; } return 0; }
輸出:
v = 100 45 78 23 220 v1 = 23 45 78 100 220
因此,此處v保持不變,並且其排序形式存儲在v1中。
相關用法
注:本文由純淨天空篩選整理自 std::partial_sort_copy in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。