• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function template
    <algorithm>

    std::partition_copy

    namespace std {
      template <class InputIterator,
                class OutputIterator1,
                class OutputIterator2,
                class Predicate>
      pair<OutputIterator1, OutputIterator2>
        partition_copy(InputIterator first,
                       InputIterator last,
                       OutputIterator1 out_true,
                       OutputIterator2 out_false,
                       Predicate pred);            // (1) C++11
    
      template <class InputIterator,
                class OutputIterator1,
                class OutputIterator2,
                class Predicate>
      constexpr pair<OutputIterator1, OutputIterator2>
        partition_copy(InputIterator first,
                       InputIterator last,
                       OutputIterator1 out_true,
                       OutputIterator2 out_false,
                       Predicate pred);            // (1) C++20
    
      template <class ExecutionPolicy,
                class ForwardIterator,
                class ForwardIterator1,
                class ForwardIterator2,
                class Predicate>
      pair<ForwardIterator1, ForwardIterator2>
        partition_copy(ExecutionPolicy&& exec,
                       ForwardIterator first,
                       ForwardIterator last,
                       ForwardIterator1 out_true,
                       ForwardIterator2 out_false,
                       Predicate pred);            // (2) C++17
    }
    

    概要

    イテレータ範囲[first, last)を条件を満たすか満たさないかで、2つの出力範囲へ分けてコピーする。

    この関数は、入力のイテレータ範囲のうち、条件を満たす要素を出力イテレータ範囲out_true、条件を満たさない要素を出力イテレータ範囲out_falseにコピーする。

    テンプレートパラメータ制約

    • InputIterator の value type は Assignable で、out_trueout_falseOutputIterator へ書き込み可能で、Predicate の引数型へ変換可能であること

    事前条件

    • 入力範囲は出力範囲のどちらとも重なっていてはならない

    効果

    イテレータ範囲[first,last) 内にあるそれぞれのイテレータ i について、pred(*i)true なら *iout_true へコピーし、そうでない場合は out_false へコピーする。

    戻り値

    first には out_true の終端が、second には out_false の終端が格納されたpairオブジェクトを返す。

    計算量

    正確に last - first述語が適用される。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <string>
    
    void print(const std::string& name, const std::vector<int>& v)
    {
      std::cout << name << " : ";
      std::for_each(v.begin(), v.end(), [](int x) {
        std::cout << x << ",";
      });
      std::cout << std::endl;
    }
    
    bool is_even(int x) { return x % 2 == 0; }
    
    int main()
    {
      std::vector<int> v = {1, 2, 3, 4, 5};
    
      // 偶数グループと奇数グループに分ける
      std::vector<int> evens;
      std::vector<int> odds;
      std::partition_copy(v.begin(), v.end(),
                          std::back_inserter(evens),
                          std::back_inserter(odds),
                          is_even);
    
      print("v", v);
      print("evens", evens);
      print("odds", odds);
    }
    

    出力

    v : 1,2,3,4,5,
    evens : 2,4,
    odds : 1,3,5,
    

    バージョン

    言語

    • C++11

    処理系

    参照