• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <algorithm>

    std::ranges::partition_copy

    namespace std::ranges {
      template <input_iterator I,
                sentinel_for<I> S,
                weakly_incrementable O1,
                weakly_incrementable O2,
                class Proj = identity,
                indirect_unary_predicate<projected<I, Proj>> Pred>
        requires indirectly_copyable<I, O1> && indirectly_copyable<I, O2>
      constexpr partition_copy_result<I, O1, O2>
        partition_copy(I first,
                       S last, O1 out_true,
                       O2 out_false,
                       Pred pred,
                       Proj proj = {}); // (1) C++20
    
      template <input_range R,
                weakly_incrementable O1,
                weakly_incrementable O2,
                class Proj = identity,
                indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
        requires indirectly_copyable<iterator_t<R>, O1> &&
                 indirectly_copyable<iterator_t<R>, O2>
      constexpr partition_copy_result<borrowed_iterator_t<R>, O1, O2>
        partition_copy(R&& r,
                       O1 out_true,
                       O2 out_false,
                       Pred pred,
                       Proj proj = {}); // (2) C++20
    }
    

    概要

    与えられた範囲を条件によって 2 つの出力の範囲へ分けてコピーする。

    事前条件

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

    効果

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

    戻り値

    o1out_true の終端、o2out_false の終端とするとき、{ .in = last, .out1 = o1, .out2 = o2 }

    計算量

    正確に 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 << " : ";
      for (int x : v) {
        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::ranges::partition_copy(v, 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++20

    処理系

    参照