template <class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);
[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; }运行截图:
该函数返回目标范围的第一个元素。(例子中的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也有一句几乎一样的话,具体需求请具体分析)
|
|
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.(first,last]
. |
|
Edit & Run
|
myvector contains: 10 20 30 10 20 30 40 50 |
[first,last)
are accessed (each object is accessed exactly once).Note that invalid arguments cause undefined behavior.
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:[email protected]
2014-9-8
于GDUT
——————————————————————————————————————————————————————————————————