STL algorithm算法copy_backward(7)

原文地址:http://www.cplusplus.com/reference/algorithm/copy_backward/
function template
<algorithm>

std::copy_backward

template <class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,
                                        BidirectionalIterator1 last,
                                        BidirectionalIterator2 result);
Copy range of elements backward
Copies the elements in the range [first,last) starting from the end into the range terminating at result.

从后往回复制[first,last)范围内的元素到result中。(即从last-1的位置开始往first方向复制元素覆盖到result的位置,覆盖也是往前移动的)

例子:

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;
void copybackward(){
    vector<int> v1{1,5,7,8,9,13};
    vector<int> v2{100,200,300};
    array<int,2> ai{888,666};

    cout<<"at first,v1=";
    for(int &i:v1)
        cout<<i<<" ";
    cout<<endl;

    cout<<"at first,v2=";
    for(int &i:v2)
        cout<<i<<" ";
    cout<<endl;




    auto it=copy_backward(v2.begin(),v2.end(),v1.end());
    cout<<"after copy_backward(v2.begin(),v2.end(),v1.end())"<<endl;
    cout<<"v1=";
    for(int &i:v1)
        cout<<i<<" ";
    cout<<endl;

     cout<<"v2=";
    for(int &i:v2)
        cout<<i<<" ";
    cout<<endl;
    cout<<"the return it is :*it="<<*it<<endl;



    cout<<endl<<"at first,ai=";
    for(int &i:ai)
        cout<<i<<" ";
    cout<<endl;


    auto it2=copy_backward(v2.begin(),v2.end(),ai.end());
    cout<<"after copy_backward(v2.begin(),v2.end(),ai.end())"<<endl;
    cout<<"ai=";
    for(int &i:ai)
        cout<<i<<" ";
    cout<<endl;

    cout<<endl;
    cout<<"the return it2 is :*it2="<<*it2<<endl;


}
运行截图:



The function returns an iterator to the first element in the destination range.

该函数返回目标范围的第一个元素。(例子中的100)


The resulting range has the elements in the exact same order as [first,last). To reverse their order, see reverse_copy.

结果的范围的顺序和[first,last)的次序一样。


The function begins by copying *(last-1) into *(result-1), and then follows backward by the elements preceding these, until first is reached (and including it).

该函数开始于将*(last-1)复制到*(result-1),然后向前移动复制,直到到达first.


The ranges shall not overlap in such a way that result (which is the past-the-end element in the destination range) points to an element in the range (first,last]. For such cases, see copy.

范围不应该指向[first,last)中的任一元素,如果需要,应该使用copy.(这句话很有问题,因为copy也有一句几乎一样的话,具体需求请具体分析


The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                         BidirectionalIterator1 last,
                                         BidirectionalIterator2 result )
{
  while (last!=first) *(--result) = *(--last);
  return result;
}



Parameters

first, last
Bidirectional iterators to the initial and final positions in a sequence to be copied. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
要复制的范围。
result
Bidirectional iterator to the past-the-end position in the destination sequence.
This shall not point to any element in the range (first,last].
要覆盖的目标数据的超尾位置。
不应该是[first,last)里的任一元素。

Return value

An iterator to the first element of the destination sequence where elements have been copied.
返回一个指向目标序列被覆盖的第一个元素。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// copy_backward example
#include <iostream>     // std::cout
#include <algorithm>    // std::copy_backward
#include <vector>       // std::vector

int main () {
  std::vector<int> myvector;

  // set some values:
  for (int i=1; i<=5; i++)
    myvector.push_back(i*10);          // myvector: 10 20 30 40 50

  myvector.resize(myvector.size()+3);  // allocate space for 3 more elements

  std::copy_backward ( myvector.begin(), myvector.begin()+5, myvector.end() );

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

  return 0;
}


Output:
myvector contains: 10 20 30 10 20 30 40 50

Complexity

Linear in the distance between first and last: Performs an assignment operation for each element in the range.

Data races

The objects in the range [first,last) are accessed (each object is accessed exactly once).
The objects in the range between the returned value and result are modified (each object is modified exactly once).

Exceptions

Throws if either an element assignment or an operation on iterators throws.

Note that invalid arguments cause undefined behavior.


——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-8

于GDUT

——————————————————————————————————————————————————————————————————






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值