algorithm - STL C++

iter_swap()

Declaration

template <class FwdIterator1, class FwdIterator2>
void iter_swap(
	FwdIterator1 xIter1,
	FwdIterator2 xIter2
);

Description

This function swaps the items that are pointed to by the iterators "xIter1" and "xIter2."

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;
	cout << "Original Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	// Swap two entries - the 'n' and the 't'
	iter_swap(qV.begin() + 5, qV.begin() + 7);

	cout << "Swapped Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

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

Output

iter_swap() Output
 

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