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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值