码迷,mamicode.com
首页 > 其他好文 > 详细

stl之std::remove_copy

时间:2015-01-06 00:41:30      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy (InputIterator first, InputIterator last,
                              OutputIterator result, const T& val);

说明:
Copies the elements in the range [first,last) to the range beginning at result, except those elements that compare equal to val.
对range进行遍历,除了==val的值,其他的copy到以result开始的位置

算法结果等价于

template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy (InputIterator first, InputIterator last,
                              OutputIterator result, const T& val)
{
  while (first!=last) {
    if (!(*first == val)) {
      *result = *first;
      ++result;
    }
    ++first;
  }
  return result;
}

用例:

// remove_copy example
#include <iostream>     // std::cout
#include <algorithm>    // std::remove_copy
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};               // 10 20 30 30 20 10 10 20
  std::vector<int> myvector (8);

  std::remove_copy (myints,myints+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout <<   << *it;
  std::cout << \n;

  return 0;
}

原文链接:http://www.cplusplus.com/reference/algorithm/remove_copy/

 

stl之std::remove_copy

标签:

原文地址:http://www.cnblogs.com/xkxjy/p/4204903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!