C++ reverse函數

reverse 在 庫中,用來翻轉 [a,b) 之間的內容

template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
     while ((first!=last)&&(first!=--last))
     {
          std::iter_swap (first,last);
          ++first;
     }
}

例如:

int a[] = {1,2,3 ,4,5,6,7};
reverse(&a[0],&a[2]);
for (int i = 0; i < 7; i++) {
    cout << a[i] << endl;
}

輸出:

2
1
3
4
5
6
7
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章