當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ Algorithm reverse()用法及代碼示例


C++算法reverse()函數用於反轉範圍[first, last)內元素的順序。

用法

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

注意:BidirectionalIterator 是一個迭代器,用於向前和向後訪問容器的任何元素。

參數

first: 一個雙向迭代器,指向元素被反轉的範圍內第一個元素的位置。

last:一個向前迭代器,指向元素被反轉的範圍內最後一個元素的位置。

返回值

複雜度

複雜度在 [first, last) 範圍內是線性的:交換元素。

數據競爭

範圍 [first, last) 中的對象被修改。

異常

如果交換元素或迭代器上的操作引發異常,則此函數將引發異常。

注意:無效的參數會導致未定義的行為。

例子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

例子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 ).

例子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

示例 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  





相關用法


注:本文由純淨天空篩選整理自 C++ Algorithm reverse()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。