共用方式為


remove_copy

複製來源範圍的項目到目的範圍,但是有一點例外,就是一個指定的項目不會複製,並不會干擾其他項目的順序和傳回新的目的範圍的結尾。

template<class InputIterator, class OutputIterator, class Type>
   OutputIterator remove_copy(
      InputIterator _First, 
      InputIterator _Last, 
      OutputIterator _Result,
      const Type& _Val
   );

參數

  • _First
    處理輸入的 Iterator 第一個項目的位置包含在項目中移除的範圍。

  • _Last
    處理輸入的 Iterator 超過最後一個項目的位置是在項目中移除的範圍。

  • _Result
    解決輸出 Iterator 的第一個項目的位置包含在項目中移除的目的範圍。

  • _Val
    要從範圍移除的值。

傳回值

沒有處理順向 Iterator 的目的範圍的新的結束位置,是透過中的序列的複本的最後一個項目的值。

備註

來源和目的範圍參考必須是有效的,任何指標必須 dereferenceable,並在序列中最後一個位置開始可取得的會增加。

必須在包含要複製的剩餘的元素之目的範圍的足夠的空間,以移除之後指定值的項目。

項目的順序未移除趨於穩定。

operator== 用來判斷在項目之間的相等必須強制在其運算元之間的一個層級的關聯性。

複雜的線性;有 (_Last – _First) 相等的比較和最多_Last (–) _First工作。

remove_copy 有兩個關聯的表單:

如需這些函式如何運作的詳細資訊,請參閱 檢查過的 Iterator

範例

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

int main() {
   using namespace std;
   vector <int> v1, v2(10);
   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 << "The original vector v1 is:     ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Remove elements with a value of 7
   new_end = remove_copy ( v1.begin( ), v1.end( ), v2.begin( ), 7 );

   cout << "Vector v1 is left unchanged as ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   cout << "Vector v2 is a copy of v1 with the value 7 removed:\n ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
   cout << ")." << endl;
}

範例輸出

The original vector v1 is:     ( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 ).
Vector v1 is left unchanged as ( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 ).
Vector v2 is a copy of v1 with the value 7 removed:
 ( 1 9 2 0 3 4 6 8 5 0 ).

需求

標題: <algorithm>

命名空間: std

請參閱

參考

remove_copy (STL Samples)

標準樣板程式庫