reverse函式 反轉容器內容

2021-07-06 08:04:51 字數 665 閱讀 5831

reverse函式可以反轉乙個容器中的內容,包含在庫中。

1、函式原型

reverse函式等同於下面的**:

template void reverse (bidirectionaliterator first, bidirectionaliterator last)

}

reverse函式使用iter_swap來交換兩個元素。

2、引數:first、last

first和last是雙向迭代器型別,reverse函式反轉的範圍是[first,last),所以包括first指向的元素,不包括last指向的元素。

3、返回值

reverse函式沒有返回值。

4、例子

// reverse algorithm example

#include // std::cout

#include // std::reverse

#include // std::vector

int main ()

輸出:

myvector contains: 9 8 7 6 5 4 3 2 1

5、複雜度

迴圈交換首尾元素。因此複雜度是線性的,並且迴圈半個陣列長度。

reverse 反轉函式

reverse用法 1.reverse函式反轉string 2.reverse函式反轉字元陣列 例子 分析與解答 reverse用法 1.reverse函式反轉string include include include using namespace std int main 例子 題目描述 輸入...

reverse 函式反轉字串以及陣列

例子 分析與解答 include include include using namespace std int main 題目描述 輸入 輸入乙個字串,以回車結束。該字串長度不超過100,包含多個單詞,單詞之間有乙個或多個空格。輸出 輸出一行,即單詞翻轉後的字串,注意每對單詞之間的空格數量保持不變...

字串反轉reverse

我們有一串字串,比如 declare source varchar max abcdefghijklmnopqrstuvwxyz 現想把它反轉顯示 zyxwvutsrqponmlkjihgfedcba此時,你可以寫乙個方法來處理 description 反轉字元 create function db...