#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
template <class T>
struct display{
void operator()(const T& x){
cout<<x<<" ";
}
};
int main(){
{
int a[] = {0,1,2,3,4,5,6,7,8};
copy_backward(a+2,a+7,a+9);//输出区间的终点与输入区间位置重叠
for_each(a,a+9,display<int>());//2,3,4,5,6,5,6,7,8
cout<<endl;
}
{
int a[] = {0,1,2,3,4,5,6,7,8};
copy_backward(a+2,a+7,a+5);//输出区间的起点与输入区间重合,可能会有问题,本例结果正确,copy算法使用memmove()执行复制操作
for_each(a,a+9,display<int>());//0 1 2 3 2 3 4 5 6
cout<<endl;
}
{
int a[] = {0,1,2,3,4,5,6,7,8};
deque<int> d (a,a+9);
deque<int>::iterator first = d.begin();
deque<int>::iterator last = d.end();
advance(first,2);//a+2
cout<<*first<<endl;//2
advance(last,-2);//a+7
cout<<*last<<endl;
deque<int>::iterator result = d.end();
copy_backward(first, last, result);
for_each(d.begin(),d.end(),display<int>());//2 3 4 5 6 5 6 7 8
cout<<endl;
}
{
int a[] = {0,1,2,3,4,5,6,7,8};
deque<int> d (a,a+9);
deque<int>::iterator first = d.begin();
deque<int>::iterator last = d.end();
advance(first,2);//a+2
//++++first;
cout<<*first<<endl;//2
advance(last,-2);//a+7
//----last;
cout<<*last<<endl;
deque<int>::iterator result = d.begin();
advance(result,5);//a+4
cout<<*result<<endl;
//输出区间的起点与输入区间重合,可能会有问题
copy_backward(first,last,result);
for_each(d.begin(),d.end(),display<int>());//0 1 2 3 2 3 2 3 2,本例结果错误,不再使用memmove()执行实际复制操作
cout<<endl;
}
return 0;
}