c++ std標準庫 演算法<algorithm> 逆轉 reverse() reverse_copy()
阿新 • • 發佈:2020-12-19
技術標籤:# 4.1 C++c++stlreverse_copyreverse
std::reverse
簡介:
將容器的序列變為當前的逆序。
函式原型
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last);
官方手冊
http://www.cplusplus.com/reference/algorithm/reverse/
std::reverse_copy
簡介:
reverse_copy() 演算法可以將源序列複製到目的序列中,目的序列中的元素是逆序的。
函式原型
template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
官方手冊
http://www.cplusplus.com/reference/algorithm/reverse_copy/
使用示例:
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
void print(int& ele)
{
cout<<ele<<", ";
}
void main()
{
int dim[]={1,2,3,4,5,6,7,8};
vector<int> v1,v2;
list<int> l1,l2;
v1.assign(dim,dim+8);
reverse(v1.begin(),v1.end());
cout<<"vector v1: " ;
for_each(v1.begin(),v1.end(),print);
cout<<endl;
copy(v1.rbegin(),v1.rend(),back_inserter(l1));
cout<<"list l1: ";
for_each(l1.begin(),l1.end(),print);
cout<<endl;
reverse(v1.begin()+2,v1.end()-2);
cout<<"vector v1: ";
for_each(v1.begin(),v1.end(),print);
cout<<endl;
list<int>::iterator posf=l1.begin();
list<int>::iterator pose=l1.end();
advance(posf,2);
advance(pose,-2);
reverse(posf,pose);
cout<<"list l1: ";
for_each(l1.begin(),l1.end(),print);
cout<<endl;
reverse_copy(v1.begin(),v1.end(),back_inserter(v2));
cout<<"vector v2: ";
for_each(v2.begin(),v2.end(),print);
cout<<endl;
cout<<"list l1: ";
reverse_copy(l1.begin(),l1.end(),ostream_iterator<int>(cout,", "));
cout<<endl;
}