vector swap() in C++ Last Updated : 05 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, std::vector::swap() is a built-in function used to exchange the contents to two vectors of same type. This function does not copy, move or swap the individual elements, instead, it swaps the internal pointers to the dynamically allocated array of both vectors and updates the size accordingly.Example: C++ // C++ program to demonstrate the use of vector::swap #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 2, 3}; vector<int> v2 = {10, 20, 30}; // Swapping contents of vec1 and vec2 v1.swap(v2); cout << "v1: "; for (int i : v1) cout << i << " "; cout << endl << "v2: "; for (int i : v2) cout << i << " "; return 0; } Outputv1: 10 20 30 v2: 1 2 3 Syntax of vector::swap()The std::vector::swap() is a member function of std::vector class defined inside <vector> header file.v1.swap(v2);Parametersv2: Second vector to be swapped. It must be of same data type as v1.Return ValueThis function does not return any value.More Examples of vector::swap()The below examples demonstrate the behaviour and side effects of using vector::swap().Example 1: Swapping Two Vectors of Strings C++ // C++ program to swap two vectors of strings #include <bits/stdc++.h> using namespace std; int main() { vector<string> v1 = {"apple", "banana", "cherry"}; vector<string> v2 = {"orange", "kiwi"}; // Swapping contents of v1 and v2 v1.swap(v2); cout << "v1: "; for (auto s : v1) cout << s << " "; cout << endl << "v2: "; for (auto s : v2) cout << s << " "; return 0; } Outputv1: orange kiwi v2: apple banana cherry Example 2: Iterator Behaviour After Using vector::swap()As std::vector::swap() function swaps just the internal pointers, the iterators to the elements of both vectors remains valid. C++ // C++ program to demonstrate iterator behavior after // vector::swap #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 2, 3}; vector<int> v2 = {10, 20, 30}; // Getting iterators before swap auto it1 = v1.begin(); auto it2 = v2.begin(); // Swapping contents of v1 and v2 v1.swap(v2); cout << "v1[0] (old v2): " << *it2 << endl; cout << "v2[0] (old v1): " << *it1; return 0; } Outputv1[0] (old v2): 10 v2[0] (old v1): 1Difference Between std::swap() and vector::swap()The std::swap() is a universal algorithm that can be used to swap the any two non-constant variable or objects. While the vector::swap() is used to swap the two vectors only.Internally, when std::swap() is called for a vector, it calls the vector::swap() to perform swapping. Comment More infoAdvertise with us Next Article Vector size() in C++ STL A abhishekcpp Follow Improve Article Tags : C++ STL cpp-vector cpp-containers-library Practice Tags : CPPSTL Similar Reads Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Vector end() in C++ STL In C++, the vector end() is a built-in method used to obtain an iterator pointing to the theoretical element after the last element of the vector. Even though this iterator does not point to a valid element, it serves as a marker for the end of the vector.Letâs take a look at an example that illustr 3 min read Vector data() in C++ STL In C++, the vector data() is a built-in function used to access the internal array used by the vector to store its elements. In this article, we will learn about vector data() in C++.Letâs take a look at an example that illustrates the vector data() method:C++#include <bits/stdc++.h> using nam 2 min read Vector size() in C++ STL In C++, the vector size() is a built-in method used to find the size of a vector. The size of a vector tells us the number of elements currently present in the vector. In this article, we will learn about the vector size() method.Let's take a look at the simple code example:C++#include <bits/stdc 3 min read Vector back() in C++ STL In C++, the vector back() is a built-in function used to retrieve the last element of the vector. It provides a reference to the last element which allows us to read or modify it directly.Letâs take a quick look at a simple example that illustrates the vector back() method:C++#include <bits/stdc+ 2 min read Vector begin() in C++ STL In C++, the vector begin() is a built-in method used to obtain an iterator pointing to the start of the vector. This iterator is used to traverse the vector or perform operations starting from the beginning of the vector.Letâs take a look at an example that illustrates the use of the vector begin() 4 min read Like