GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::partition_copy - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. algorithm
  3. partition_copy

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function template
<algorithm>

std::partition_copy(C++11)

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 は CopyAssignable で、out_true と out_false の OutputIterator へ書き込み可能で、Predicate の引数型へ変換可能であること

事前条件

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

効果

イテレータ範囲[first,last) 内にあるそれぞれのイテレータ i について、pred(*i) が true なら *i を out_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

処理系

  • Clang: 9.0.1 ✅
  • GCC: 4.7.0 ✅
  • Visual C++: 2010 ✅, 2012 ✅, 2013 ✅, 2015 ✅

関連項目

  • ranges::partition_copy

参照

  • N2569 More STL algorithms
  • N2666 More STL algorithms (revision 2)
  • P0202R3 Add Constexpr Modifiers to Functions in <algorithm> and <utility> Headers
  • P0467R2 Iterator Concerns for Parallel Algorithms
  • LWG Issue 2357. Remaining "Assignable" requirement
    • C++14で、要件の名前が、C++98から使われていたAssignableからCopyAssignableへ改められた
    • この修正は欠陥報告(DR)であり、C++11以降に遡及して適用される。C++11で名前付き要件を改名した際に本関数への適用が漏れた編集上の誤りの修正であり、要件の内容そのものは変わらないため
  • LWG Issue 4465. §[alg.partitions] Clarify Returns: element
    • C++26で、戻り値のpairが、それぞれout_true/out_falseの各出力範囲へコピーされた最後の要素の次を指すイテレータであることが明確化された
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.