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