template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last)) {
std::iter_swap (first,last);
++first;
}
}
将[first,last)中的数组元素倒序
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[] = { 1,2,3,4,5,6 };
reverse(a, a + 6);
return 0;
}
结果为