I l@ve RuBoard Previous Section Next Section

rotate(), rotate_copy()

rotate() is passed three iterators: first, middle, and last. It exhanges the two ranges marked by the iterators first, middle-1 and middle, last-1. For example, given the following C-style character string "boohiss!!",



char ch[] = "boohiss!!"; 

To change it to "hissboo!!", the call to rotate() looks like this:



rotate( ch, ch+3, ch+7 ); 

Here is another example:



#include <algorithm> 


int ia[] = { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 10 }; 


vector<int> vec( ia, ia+11 ), vec2(11); 

In this first invocation, we exchange the last six elements, beginning with 0, with the first five elements, beginning with 1:



// rotate on middle element(0) : 0 2 4 6 8 10 1 3 5 7 9 


rotate( ia, ia+5, ia+11 ); 

In this second invocation, we exchange the last two elements, beginning with 8, with the first nine elements, beginning with 1:



// rotate on next to last element(8): 8 10 1 3 5 7 9 0 2 4 6 


rotate_copy( vec.begin(), vec.end()-2, vec.end(), vec2.begin() ); 
    I l@ve RuBoard Previous Section Next Section