C++ 算法 中的 reverse函数

首页 / C++入门教程 / C++ 算法 中的 reverse函数

C++算法 reverse()函数用于在[first,last)参数内反转元素的顺序。

reverse - 语法

template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last);

Note: BidirectionalIterator是一个迭代器,用于以正向和反向访问容器的任何元素。

reverse - 参数

first:一个双向迭代器,它指向第一个元素在元素反转参数内的位置。

last:前向迭代器,在元素反转参数内,将位置指向最后一个元素之后的位置。

reverse - 返回值

没有

reverse - 例子1

让我们看一个简单的例子来反转给定的字符串:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string str = "Hello Myself Nikita";
  cout << "Before Reverse : "<< str << endl;

  reverse(str.begin(), str.end());
  cout <<"After Reverse  : " << str << endl;
  
  return 0;
}

输出:

Before Reverse : Hello Myself Nikita
After Reverse   : atikiN flesyM olleH

reverse - 例子2

让我们看另一个简单的例子来反转数字参数:

#include <vector>  
#include <algorithm>  
#include <iostream>  

using namespace std;
  
int main( ) {    
   vector <int> v1;  
   vector <int>::iterator Iter1;  
  
   int i;  
   for ( i = 0 ; i <= 9 ; i++ )  
   {  
      v1.push_back( i );  
   }  
  
   cout << "The original vector v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
  //Reverse the elements in the vector   
   reverse (v1.begin( ), v1.end( ) );  
  
   cout << "The modified vector v1 with values reversed is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}  

输出:

The original vector v1 is:
 ( 0 1 2 3 4 5 6 7 8 9 ).
The modified vector v1 with values reversed is:
 ( 9 8 7 6 5 4 3 2 1 0 ).

reverse - 例子3

让我们看另一个简单的例子:

#include<iostream> 
#include<algorithm> 
#include<vector> 
using namespace std; 
  
int main() 
{ 
    vector <int> v ; 
      
   //Inserting elements in vector 
    for (int i = 0; i < 8; i++) 
        v.push_back(i+10); 
        
   //Displaying elements of vector 
    vector <int> ::iterator it; 
    cout<<"Before: ";
    for (it = v.begin(); it != v.end(); it++) 
        cout << (*it) << " "; 
      
    cout << "\n\nReverse only from index 5 to 7 in array:\n"; 
   //Reversing elements from index 5 to index 7 
    reverse(v.begin() + 5, v.begin() + 8); 
      
    for (it = v.begin(); it != v.end(); it++) 
        cout << (*it) << " "; 
      
   //Reversing directly from beginning to end 
    cout << "\nReverse full array:\n"; 
      
    int a[] = {4, 5, 6, 7}; 
    reverse(begin(a), end(a)); 
  
   //Print the array 
    cout << a[0] << a[1] << a[2] << a[3] << '\n'; 
    return 0; 
} 

输出:

Before: 10 11 12 13 14 15 16 17 

Reverse only from index 5 to 7 in array:
10 11 12 13 14 17 16 15 
Reverse full array:
7654

reverse - 例子4

让我们看另一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
 
void print(string a[], int N)
{   
    for(int i = 0; i < N; i++)
    {
        cout << (i + 1) << ". " << setw(5)
             << a[i] << "  ";
    }
    cout << endl;
}
 
int main()
{
    string s[] = {"George", "John", "Nik", "Alice", "Bob", "Watson"};
 
    cout << "Original order : ";
    print(s, 6);
    cout << "\nReversing the order ... " << endl;
    reverse(s, s + 6);
    cout << "Reversed order : ";
    print(s, 6);
}

输出:

Original order : 1. George  2.  John  3.   Nik  4. Alice  5.   Bob  6. Watson  

Reversing the order ....
Reversed order : 1. Watson  2.   Bob  3. Alice  4.   Nik  5.  John  6. George  

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

硅谷产品实战36讲 -〔曲晓音〕

邱岳的产品实战 -〔邱岳〕

Android开发高手课 -〔张绍文〕

即时消息技术剖析与实战 -〔袁武林〕

高并发系统设计40问 -〔唐扬〕

现代C++编程实战 -〔吴咏炜〕

.NET Core开发实战 -〔肖伟宇〕

容量保障核心技术与实战 -〔吴骏龙〕

好记忆不如烂笔头。留下您的足迹吧 :)