【STL】reverse函數用法


reverse函數的功能是反轉排序一個容器中指定元素的內容。

函數參數:reverse(first,last),其中firstlast分別指向被反轉序列中初始及末尾位置的雙向迭代器(Bidirectional iterators)。這個範圍即 [first,last) ,包括 first 到 last 間的全部元素,包括 first 指向的元素,但不包括 last 指向的元素。ios

無返回值安全

示例:函數

#include <iostream>
#include <list>
 
namespace ClassFoo{
void ListReverseExample1() {
    std::list<int> foo;
 
    for (int i=1; i<10; ++i) foo.push_back(i);
 
    foo.reverse();
 
    std::cout << "foo contains:";
    for (std::list<int>::iterator it=foo.begin();
        it!=foo.end(); ++it)
        std::cout << ' ' << *it;
 
    std::cout << '\n';
}
}
int main ()
{
    ClassFoo::ListReverseExample1();
    return 0;
}

 

輸出  spa

foo contains:9 8 7 6 5 4 3 2 1code

複雜度blog

O(n),n 爲 last - first.。排序

數據爭用相關it

範圍 [first,last) 中的全部元素都被修改過。io

異常安全性相關ast

若是元素交換(Swap)或操做某個迭代器拋異常,該函數纔會拋異常。

相關文章
相關標籤/搜索