若要檢視英文版的文章，請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上，即可在快顯視窗顯示英文原文。 譯文 原文

# remove_if

Visual Studio 2015

## 語法

```
template<class ForwardIterator, class Predicate>
ForwardIterator remove_if(
ForwardIterator _First,
ForwardIterator _Last,
Predicate _Pred
);
```

_First

_Last

_Pred

## 備註

operator== 可用來判斷項目之間的相等必須安排在其運算元之間的等價關聯。

## 範例

```// alg_remove_if.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

bool greater6 ( int value ) {
return value >6;
}

int main( ) {
using namespace std;
vector <int> v1, v2;
vector <int>::iterator Iter1, Iter2, new_end;

int i;
for ( i = 0 ; i <= 9 ; i++ )
v1.push_back( i );

int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
v1.push_back( 7 );

random_shuffle ( v1.begin( ), v1.end( ) );
cout << "Vector v1 is ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// Remove elements satisfying predicate greater6
new_end = remove_if (v1.begin( ), v1.end( ), greater6 );

cout << "Vector v1 with elements satisfying greater6 removed is\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// To change the sequence size, use erase
v1.erase (new_end, v1.end( ) );

cout << "Vector v1 resized elements satisfying greater6 removed is\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
}
```

## 範例輸出

```Vector v1 is ( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 ).
Vector v1 with elements satisfying greater6 removed is
( 1 2 0 3 4 6 5 3 4 6 8 5 7 7 ).
Vector v1 resized elements satisfying greater6 removed is
( 1 2 0 3 4 6 5 ).
```