algorithm - STL C++

rotate()

Declaration

template <class ForwardIterator>
void rotate(
	ForwardIterator xFirst,
	ForwardIterator xMiddle,
	ForwardIterator xLast
);

Description

This function rotates the range of entries starting at "xFirst" up to the one before "xMiddle" into the range up to the entry before "xLast." The result works a like a bitwise roll.

Header Include

#include <algorithm>

Example

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	using namespace std;

	// Create a vector instance
	vector<char> qV;
	qV.push_back('X');
	qV.push_back('o');
	qV.push_back('a');
	qV.push_back('X');
	qV.push_back('.');
	qV.push_back('n');
	qV.push_back('e');
	qV.push_back('t');

	vector<char>::iterator qIter;
	// Output the vector
	cout << "Original: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	// Rotate the first six entries
	rotate(qV.begin(), qV.begin() + 4,  qV.begin() + 6);

	// Output resulting vector
	cout << "Result  : ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

rotate() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.