变序性算法改变元素的次序,但不改变元素值。

这些算法不能用于关联式容器,因为在关联式容器中,元素有一定的次序,不能随意变动。

 

逆转元素次序

void

reverse(BidirectionalIterator beg,BidirectionalIterator end)

OutputIterator

reverse_copy(BidirectionalIterator sourceBeg,BidirectionalIterator sourceEnd,

                      OutputIterator destBeg)

1.reverce()会将区间[beg,end)内的元素全部逆序

2.reverse_copy()是reverse()跟copy()的组合

 

下面这个程序展示reverse()和reverse_copy()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     vector<int> coll;
 8     INSERT_ELEMENTS(coll,1,9);
 9     PRINT_ELEMENTS(coll,"coll: ");
10     reverse(coll.begin(),coll.end());
11     PRINT_ELEMENTS(coll,"coll: ");
12     reverse(coll.begin()+1,coll.end()-1);
13     PRINT_ELEMENTS(coll,"coll: ");
14     reverse_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
15     cout<<endl;
16 }
View Code

相关文章:

  • 2022-12-23
  • 2021-12-14
  • 2021-12-03
  • 2022-02-01
  • 2021-07-02
  • 2022-12-23
  • 2021-12-18
  • 2021-06-01
猜你喜欢
  • 2021-12-26
  • 2021-10-28
  • 2021-12-03
  • 2022-02-16
  • 2021-10-31
  • 2021-08-14
  • 2021-10-15
相关资源
相似解决方案