YetiCodeCamp - C++ for Beginners

Companion website for the C++ for Beginners YouTube video series.

C++ Vector swap() Function


The C++ vector.swap() function exchanges the elements specified in two vectors. This function takes another vector as a parameter.

Syntax:

Example, using vectors “v1” and “v2”.

v1.swap(v2);

Swaps the contents of v1 with that of v2.





This example is a complete program using the vector swap() function to swap the contents of v1 with v2.

#include<iostream>
#include<vector>

void prtVec(const std::vector<auto> &vec, std::string n="")
{
        if (n!="")
            std::cout << n << " is: ";
        for(auto v : vec)
            std::cout << v << " ";
        std::cout << "\n" <<std::endl;
}

int main()
{
std::vector<int> v1 {1,2,3,4,5,6};
std::vector<int> v2 {94,95,96,97,98,99};

std::cout << "Before swap:\n\n";
prtVec(v1, "v1");
prtVec(v2, "v2");

v1.swap(v2);

std::cout << "After swap:\n\n";
prtVec(v1, "v1");
prtVec(v2, "v2");

return 0;
}

Output:

Before swap:

v1 is: 1 2 3 4 5 6

v2 is: 94 95 96 97 98 99

After swap:

v1 is: 94 95 96 97 98 99

v2 is: 1 2 3 4 5 6