天天看點

C++文法基礎--泛型算法(generic algorithm)--replace_copy(),unique_copy,copy()

         注:泛型算法系列筆記是寫給自己看的。是以做整理時保留了英文的解釋。

1.replace_copy

  Copies the elements in the range [first,last) to the range beginning at result, replacing the appearances of old_value by new_value.

 原型:

template <class InputIterator, class OutputIterator, class T>

  OutputIterator replace_copy (InputIterator first, InputIterator last,

                               OutputIterator result,

                               const T& old_value, const T& new_value);

該算法等效于:

 template <class InputIterator, class OutputIterator, class T>

  OutputIterator replace_copy (InputIterator first, InputIterator last,

                               OutputIterator result, const T& old_value, const T& new_value)

{

  while (first!=last) {

    *result = (*first==old_value)? new_value: *first;

    ++first; ++result;

  }

  return result;

}

 解析:

  first, last:Input iterators to the initial and final positions in a sequence.

  result:Output iterator to the initial position of the range where the resulting sequence is stored. 

  old_value:Value to be replaced.

  new_value:Replacement value.

  Return:An iterator pointing to the element that follows the last element written in the result sequence.

Example: 

 int main ()

{

  int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };

  vector<int> myvector (8);

  replace_copy (myints, myints+8, myvector.begin(), 20, 99);//把所有20換成99

  // myvector contains:

  for (int x:myvector)

  {

std::cout << ' ' << x;//10 99 30 30 99 10 10 99

  }

  return 0;

}

2.unique_copy

  Copies the elements in the range [first,last) to the range beginning at result, 

   except consecutive duplicates (elements that compare equal to the element preceding).

  原型:

   equality :

template <class InputIterator, class OutputIterator>

  OutputIterator unique_copy (InputIterator first, InputIterator last,

                              OutputIterator result);

 predicate :

template <class InputIterator, class OutputIterator, class BinaryPredicate>

  OutputIterator unique_copy (InputIterator first, InputIterator last,

                              OutputIterator result, BinaryPredicate pred);

 解析:

  first, last:Forward iterators to the initial and final positions in a sequence.

  result:Output iterator to the initial position of the range where the resulting range of values is stored.

  pred:Binary function that accepts two elements in the range as argument, and returns a value convertible to bool. 

  return: An iterator pointing to the end of the copied range, which contains no consecutive duplicates.

 該算法等效于:

  template <class InputIterator, class OutputIterator>

  OutputIterator unique_copy (InputIterator first, InputIterator last,

                              OutputIterator result)

{

  if (first==last) return last;

  *result = *first;

  while (++first != last) {

    typename iterator_traits<InputIterator>::value_type val = *first;

    if (!(*result == val))   // or: if (!pred(*result,val)) for version (2)

      *(++result)=val;

  }

  return ++result;

}

Example:

int main()

{

int arr[]={1,2,3,2,1,2};

vector<int> vec(arr,arr+6);

vector<int> vec1;

sort(vec.begin(),vec.end());

       unique_copy(vec.begin(),vec.end(), back_inserter(vec1),

                            [](int a, int b)->int{ return a==b; });   //lambda表達式  [](int a, int b)->int{ return a==b; }

for(int x:vec1)                                                 //相當于 int (int a,intb){ return a==b;}

{

cout<<x<<'\t';//1 2 3

}

}

3.copy

    Copies the elements in the range [first,last) into the range beginning at result.

 原型:

  template <class InputIterator, class OutputIterator>

  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);

 該算法等效于:

 template<class InputIterator, class OutputIterator>

  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)

{

  while (first!=last) {

    *result = *first;

    ++result; ++first;

  }

  return result;

}

 Example:

 int main () 

{

  int myints[]={1,2,3,4,5};

  vector<int> myvector (5);

  copy ( myints, myints+5, myvector.begin() );

  for(int x:myvector)

  {

 cout<<x<<'\t'; //1 2 3 4 5

  }

  return 0;

}

繼續閱讀