STLalgorithm算法reverse,reverse_copy(50)
reverse原型:
<span style="font-size:18px;">std::reverse template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last);</span>
反转范围内的元素。
行为类似于:
template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last) { while ((first!=last)&&(first!=--last)) { std::iter_swap (first,last); ++first; } }
reserve_copy原型:
std::reverse_copy
template <class BidirectionalIterator, class OutputIterator> OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
将范围内的元素以逆序的顺序存放到result开始的位置。
返回值为最后一个被覆盖元素的下一个位置的迭代器。
行为类似于:
template <class BidirectionalIterator, class OutputIterator> OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result) { while (first!=last) { --last; *result = *last; ++result; } return result; }
一个简单的例子:
#include <iostream> #include <algorithm> #include <vector> using namespace std; void reversecopy() { vector<int> vi{1,2,3,4,4,6}; vector<int> vresult(6); cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"vreult="; for(int i:vresult) cout<<i<<" "; cout<<endl; reverse(vi.begin(),vi.end()); cout<<"after reverse(vi.begin(),vi.end())"<<endl; cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; reverse_copy(vi.begin(),vi.end(),vresult.begin()); cout<<"after reverse_copy(vi.begin(),vi.end(),vresult.begin());"<<endl; cout<<"vreult="; for(int i:vresult) cout<<i<<" "; cout<<endl; }
运行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-25
于GDUT
——————————————————————————————————————————————————