Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

reverse_copy

函数模板  <algorithm>
template <class BidirectionalIterator, class OutputIterator>
  OutputIterator reverse_copy (BidirectionalIterator first,
                               BidirectionalIterator last, OutputIterator result);

以相反顺序复制范围的元素

将范围[first,last)中的元素复制到以result开始的范围,但顺序相反。

这个函数模板的行为相当于:
template <class BidirectionalIterator, class OutputIterator>
  OutputIterator reverse_copy (BidirectionalIterator first,
                               BidirectionalIterator last, OutputIterator result)
{
  while (first!=last) {
    --last;
    *result = *last;
    ++result;
  }
  return result;
}


☲  参数


first, last
指向一个要反转序列的初始和最终位置的双向迭代器. 使用的范围是[first,last), 它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。

双向迭代器(BidirectionalIterator)应该指向一个正确定义了swap的类型。

result
指向一个要存储反向范围的开始的输出迭代器. 指定类型应该支持在[first,last)范围内分配元素的值。
范围不得重叠.

☉  返回值



指向结果范围末尾的输出迭代器,其中包含按相反顺序排列的相同元素。

☣  示例



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

int main () {
  int myints[] ={1,2,3,4,5,6,7,8,9};
  std::vector<int> myvector;

  myvector.resize(9);    // allocate space

  std::reverse_copy (myints, myints+9, myvector.begin());

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

输出:
myvector contains: 9 8 7 6 5 4 3 2 1

✥ 复杂度



first1和last1之间的距离线性:为每个元素执行赋值操作。

⇄ 数据竞争


访问[first,last)范围内的对象。结果和返回值之间的对象被修改。 每个对象只被访问一次。

☂ 异常安全性



如果迭代器上的元素赋值或操作抛出,则抛出。
注意,无效的参数会导致未定义的行为。

🍄  另请参阅



reverse 反转元素(函数模板)
rotate_copy 旋转并复制元素(函数模板)
copy 复制范围的元素(函数模板)
copy_backward 从末尾复制范围的元素(函数模板)
swap 交换两个对象的值(函数模板)

联系我们 免责声明 关于CandCplus 网站地图