C++算法 partition_copy()函数用于将条件为true的元素复制到一个目标,并将条件为false的元素复制到另一个目标。元素必须属于指定参数。
template <class InputIterator, class OutputIterator1, class OutputIterator2, class UnaryPredicate pred> pair<OutputIterator1,OutputIterator2> partition_copy (InputIterator first, InputIterator last, OutputIterator1 result_true, OutputIterator2 result_false, UnaryPredicate pred);
first:一个输入迭代器,它指向参数中的第一个元素以检查条件。
last:一个输入迭代器,它指向参数的最后一个元素。
pred :用户定义的一元谓词函数,用于定义要测试的条件。
result_true :一个输出迭代器,用于复制pred返回true的元素。
result_false :一个输出迭代器,用于复制pred返回false的元素。
此函数返回一对迭代器,分别以result_true和result_false寻址所生成序列的末尾。
让我们看一个简单的示例来演示partition_copy()的用法:
#include <iostream> //std::cout #include <algorithm> //std::partition_copy, std::count_if #include <vector> //std::vector using namespace std; bool IsOdd (int i) { return (i%2)==1; } int main () { vector<int> foo {1,2,3,4,5,6,7,8,9}; vector<int> odd, even; //resize vectors to proper size: unsigned n = count_if (foo.begin(), foo.end(), IsOdd); odd.resize(n); even.resize(foo.size()-n); //partition: partition_copy (foo.begin(), foo.end(), odd.begin(), even.begin(), IsOdd); //print contents: cout << "odd is: "; for (int& x:odd) cout << ' ' << x; cout << '\n'; cout << "even is: "; for (int& x:even) cout << ' ' << x; cout << '\n'; return 0; }
输出:
odd is: 1 3 5 7 9 even is: 2 4 6 8
让我们看另一个简单的例子:
#include <iostream> #include <algorithm> #include <utility> using namespace std; int main() { int arr [10] = {1,2,3,4,5,6,7,8,9,10}; int true_arr [5] = {0}; int false_arr [5] = {0}; partition_copy(begin(arr), end(arr), begin(true_arr),begin(false_arr), [] (int i) {return i > 5;}); cout << "true_arr: "; for (int x : true_arr) { cout << x << ' '; } cout << '\n'; cout << "false_arr: "; for (int x : false_arr) { cout << x << ' '; } cout << '\n'; return 0; }
输出:
true_arr: 6 7 8 9 10 false_arr: 1 2 3 4 5
让我们看另一个简单的例子:
#include <iostream> #include <vector> #include <algorithm> #include <iterator> #include <string> using namespace std; void print(const string& name, const vector<int>& v) { cout << name << " : "; for_each(v.begin(), v.end(), [](int x) { cout << x << ","; }); cout << endl; } bool is_even(int x) { return x % 2 == 0; } int main() { vector<int> v = {1, 2, 3, 4, 5}; vector<int> evens; vector<int> odds; partition_copy(v.begin(), v.end(), back_inserter(evens), back_inserter(odds), is_even); print("v", v); print("evens", evens); print("odds ", odds); return 0; }
输出:
v : 1,2,3,4,5, evens : 2,4, odds : 1,3,5,
让我们看另一个简单的例子:
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool is_even(int num) { return (num % 2 == 0); } void print_vector(const vector<int> & VecRef) { cout << endl; cout << "Contents of the vector is : " << endl; for (auto it = VecRef.begin(); it != VecRef.end(); ++it) { cout << "[" << it - VecRef.begin() << "] : " << *it << endl; } } int main() { vector<int> NumbersVec; for (int cnt = 1; cnt < 10; ++cnt) NumbersVec.push_back(cnt); vector<int> EvenVec, OddVec; unsigned countEven = count_if(NumbersVec.begin(), NumbersVec.end(), is_even); cout << "Evens : " << countEven << endl; //Prints 4 cout << "Odds : " << NumbersVec.size() - countEven << endl; //Prints 5 EvenVec.resize(countEven); OddVec.resize(NumbersVec.size() - countEven); partition_copy(NumbersVec.begin(), NumbersVec.end(), EvenVec.begin(), OddVec.begin(), is_even); //partition_copy(NumbersVec.begin(), NumbersVec.end(), back_inserter(EvenVec), back_inserter(OddVec), is_even); This works... print_vector(EvenVec); print_vector(OddVec);//<== this one crashes return 0; }
输出:
Evens : 4 Odds : 5 Contents of the vector is : [0] : 2 [1] : 4 [2] : 6 [3] : 8 Contents of the vector is : [0] : 1 [1] : 3 [2] : 5 [3] : 7 [4] : 9
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Tony Bai · Go语言第一课 -〔Tony Bai〕