vector - STL C++

swap()

Declaration

template <class X, class A>
bool swap(vector<X, A>& qrV1, vector<X, A>& qrV2);

Description

This is the friend function swap() for vectors, and it swaps the set of entries in the two vectors qrV1 and qrV2.

Header Include

#include <vector>

Example

#include <iostream>
#include <vector>

int main()
{
	using namespace std;

	// Create a two vector instances and add bits to them
	vector<bool> qV1;
	qV1.push_back(true);
	qV1.push_back(false);
	qV1.push_back(false);
	qV1.push_back(true);

	vector<bool> qV2;
	qV2.push_back(false);
	qV2.push_back(true);

	// Output the vectors and the swapped version
	for (int i = 0; i < 2; ++i) {
		cout << "V1  = ";
		vector<bool>::iterator qIter;
		for (qIter = qV1.begin(); qIter != qV1.end(); ++qIter) {
			cout << *qIter;
		}
		cout << endl;
		cout << "V2  = ";
		for (qIter = qV2.begin(); qIter != qV2.end(); ++qIter) {
			cout << *qIter;
		}
		cout << endl;
		if (i == 0) {
			cout << "swap vectors" << endl;
		}
		swap(qV1, qV2);
	}

	cin.get();
	return 0;
}

Output

swap() Output
 

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