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::copy_if - cpprefjp C++日本語リファレンス

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

最終更新日時: 2026年04月13日 15時17分49秒 (JST)
teruyamato0731 が更新

履歴 編集

function template
<algorithm>

std::copy_if(C++11)

namespace std {
  template <class InputIterator, class OutputIterator, class Predicate>
  OutputIterator
    copy_if(InputIterator first,
            InputIterator last,
            OutputIterator result,
            Predicate pred);         // (1) C++11

  template <class InputIterator, class OutputIterator, class Predicate>
  constexpr OutputIterator
    copy_if(InputIterator first,
            InputIterator last,
            OutputIterator result,
            Predicate pred);         // (1) C++20

  template <class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
            class Predicate>
  ForwardIterator2
    copy_if(ExecutionPolicy&& exec,
            ForwardIterator1 first,
            ForwardIterator1 last,
            ForwardIterator2 result,
            Predicate pred);         // (2) C++17
}

概要

イテレータ範囲[first, last)のうち、条件を満たす要素のみを出力イテレータ範囲にコピーする。

要件

イテレータ範囲[first,last)と、イテレータ範囲[result,result + (last - first))は重なっていてはならない。

効果

[first,last) 内のイテレータ i について pred(*i) が true である要素を result へ順番にコピーする。

戻り値

コピー先のイテレータ範囲の終端を返す。

計算量

正確に last - first 回述語を適用する。

備考

このコピーは安定なコピーである。つまり、コピーによって要素の前後が入れ替わることは無い。

例

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>

bool isOdd(int x) { return x % 2 != 0; }

int main() {
  std::vector<int> v1 = { 3, 1, 4 };
  std::vector<int> v2 = { 1, 5, 9 };
  std::vector<int> v3 = { 2, 6, 5 };
  std::vector<int> result(v1.size() + v2.size() + v3.size());

  // copy_if の戻り値を使って、複数のコンテナにある奇数を全て繋げる
  auto out = result.begin();
  out = std::copy_if(v1.begin(), v1.end(), out, isOdd);
  out = std::copy_if(v2.begin(), v2.end(), out, isOdd);
  out = std::copy_if(v3.begin(), v3.end(), out, isOdd);

  std::copy(result.begin(), out, std::ostream_iterator<int>(std::cout, ","));
}

出力

3,1,1,5,9,5,

実装例

template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator copy_if(InputIterator first, InputIterator last,
                       OutputIterator result, Predicate pred) {
  for ( ; first != last; ++first)
    if (pred(*first))
      *result++ = *first;
  return result;
}

バージョン

言語

  • C++11

処理系

  • Clang: 3.0 ✅
  • GCC: 4.4.7 ✅
  • Visual C++: 2010 ✅, 2012 ✅, 2013 ✅, 2015 ✅

関連項目

  • ranges::copy_if

参照

  • N2569 More STL algorithms
  • N2666 More STL algorithms (revision 2)
  • LWG Issue 2039. Issues with std::reverse and std::copy_if
    • C++11まで、戻り値の仕様がなかった。C++14で追加。
  • P0202R3 Add Constexpr Modifiers to Functions in <algorithm> and <utility> Headers
  • P0467R2 Iterator Concerns for Parallel Algorithms
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.