// rotate.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <algorithm> #include <vector> #include <iostream> using namespace std; /*****************************************************************************/ template<class _FI, class _OI> inline _OI rotate_copy(_FI _F, _FI _M, _FI _L, _OI _X) { return (copy(_F, _M, copy(_M, _L, _X))); } /*****************************************************************************/ template<class _RI, class _Pd, class _Ty> inline void my_Rotate(_RI _F, _RI _M, _RI _L, _Pd *, _Ty *) { _Pd _D = _M - _F; _Pd _N = _L - _F; for (_Pd _I = _D; _I != 0; ) { _Pd _J = _N % _I; _N = _I, _I = _J; } if (_N < _L - _F) for (; 0 < _N; --_N) { _RI _X = _F + _N; _RI _Y = _X; _Ty _V = *_X; _RI _Z = _Y + _D == _L ? _F : _Y + _D; while (_Z != _X) { *_Y = *_Z; _Y = _Z; _Z = _D < _L - _Z ? _Z + _D : _F + (_D - (_L - _Z)); } *_Y = _V; } } /*****************************************************************************/ /* * ハ莎・ original element sequence: 1 3 5 7 9 0 2 4 6 8 10 rotate on middle element(0) :: 0 2 4 6 8 10 1 3 5 7 9 rotate on next to last element(8) :: 8 10 1 3 5 7 9 0 2 4 6 rotate_copy on middle element :: 7 9 0 2 4 6 8 10 1 3 5 */ int main(int argc, char* argv[]) { int ia[] = { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 10 }; vector< int > vec( ia, ia+11 ); ostream_iterator< int > ofile( cout, " " ); cout << "original element sequence:/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; rotate( &ia[0], &ia[5], &ia[11] ); cout << "rotate on middle element(0) ::/n"; copy( ia, ia+11, ofile ); cout << '/n'; rotate( vec.begin(), vec.end()-2, vec.end() ); cout << "rotate on next to last element(8) ::/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; vector< int > vec_res( vec.size() ); rotate_copy( vec.begin(), vec.begin()+vec.size()/2, vec.end(), vec_res.begin() ); cout << "rotate_copy on middle element ::/n"; copy( vec_res.begin(), vec_res.end(), ofile ); cout << '/n'; return 0; }