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

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

最終更新日時: 2026年08月25日 12時11分17秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<algorithm>

std::remove_if

namespace std {
  template <class ForwardIterator, class Predicate>
  ForwardIterator
    remove_if(ForwardIterator first,
              ForwardIterator last,
              Predicate pred);        // (1) C++98

  template <class ForwardIterator, class Predicate>
  constexpr ForwardIterator
    remove_if(ForwardIterator first,
              ForwardIterator last,
              Predicate pred);        // (1) C++20

  template <class ExecutionPolicy, class ForwardIterator, class Predicate>
  ForwardIterator
    remove_if(ExecutionPolicy&& exec,
              ForwardIterator first,
              ForwardIterator last,
              Predicate pred);        // (2) C++17
}

概要

イテレータ範囲[first, last)から条件を満たす要素を除ける。

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

  • *first の型は MoveAssignable の要件を満たすこと

効果

[first,last) 内にあるイテレータ i について、pred(*i) != false である要素を取り除き、有効な要素を範囲の前に寄せる。

戻り値

有効な要素の末尾の次を指すイテレータを返す。

計算量

正確に last - first 回の述語の適用を行う

備考

安定。

備考

有効な要素を範囲の前方に集める処理には、ムーブを使用する。

取り除いた要素の先頭を指すイテレータをretとし、範囲[ret, last)の各要素には、有効な要素からムーブされた値が設定される。それらの値は、「有効だが未規定な値」となる。

例

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

int main() {
  std::vector<int> v = { 2,3,1,2,1 };

  auto result = std::remove_if(v.begin(), v.end(),
    [](int x) { return x%2 != 0; });

  // [v.begin(),result) の範囲に奇数を除去した結果が入っている
  std::for_each(v.begin(), result, [](int x) { std::cout << x << ","; });
  std::cout << std::endl;

  // remove を使ってもコンテナの要素数は変わらないことに注意しよう
  std::cout << "size before: " << v.size() << std::endl;

  // [v.begin(),result) の範囲に奇数を除去した結果が入っているので、
  // [result,v.end()) を erase することでサイズも変更することができる
  // (Erase-remove イディオム)
  v.erase(result, v.end());
  std::cout << "size after: " << v.size() << std::endl;
}

出力

2,2,
size before: 5
size after: 2

実装例

template <class ForwardIterator, class Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred) {
  auto result = first;
  for ( ; first != last; ++first)
    if (!pred(*first))
      if (first == result)
        ++result;
      else
        *result++ = std::move(*first);
  return result;
}

関連項目

  • ranges::remove_if

参照

  • More C++ Idioms/消去・削除(Erase-Remove)
  • LWG Issue 2110. remove can't swap but note says it might
    • C++11までのこのアルゴリズムは、要素の移動にswap操作が行われるかもしれない、と書いていた。だが、このアルゴリズムの要件はMoveAssignableのみであるため、swapはできない。そのため、C++14からは、ムーブのみで要素の移動が行われるようになった。
  • P0202R3 Add Constexpr Modifiers to Functions in <algorithm> and <utility> Headers
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.