C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ partition_copy()

C++算法partition_copy()

C++算法 partition_copy()函数用于将条件为真的元素复制到一个目标,并且对另一个人虚假。元素必须属于指定范围。

语法

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寻址

复杂度

如果有足够的内存,则复杂度在[first,last)范围内是线性的: 将pred应用于每个元素并为每个元素执行分配。

数据竞争

访问范围为[first,last)的对象,其中每个元素仅被访问一次。

异常

如果pred,元素的赋值或迭代器上的任何操作抛出异常,则此函数将引发异常。
注意: 无效的参数会导致未定义的行为。

示例1

让我们看一个简单的示例来演示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

示例2

让我们看另一个简单的示例:
#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

示例3

让我们看另一个简单的示例:
#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,

示例4

让我们看另一个简单的示例:
#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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4