rotate是一个比较有用的算法函数,我们可以使用它来交换前后数据,请看如下,我们以5为中间点
0 1 2 3 4 5 6 7 8 9
|| rotate
//
5 6 7 8 9 0 1 2 3 4
实现程序如下所示:
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
using namespace std;
template <typename T>
struct SVisit: public unary_function<T, void>
{
inline void operator()( const T& t ) const
{
cout << t << endl;
}
};
int main( void )
{
list<int> il;
for ( int i = 0; i < 10; ++ i )
{
il.push_back( i );
}
rotate( il.begin(), find( il.begin(), il.end(), 5 ), il.end() );
for_each( il.begin(), il.end(), SVisit<int>() );
return 0;
}
0 1 2 3 4 5 6 7 8 9
|| rotate
//
5 6 7 8 9 0 1 2 3 4
实现程序如下所示:
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
using namespace std;
template <typename T>
struct SVisit: public unary_function<T, void>
{
inline void operator()( const T& t ) const
{
cout << t << endl;
}
};
int main( void )
{
list<int> il;
for ( int i = 0; i < 10; ++ i )
{
il.push_back( i );
}
rotate( il.begin(), find( il.begin(), il.end(), 5 ), il.end() );
for_each( il.begin(), il.end(), SVisit<int>() );
return 0;
}