std::swap_ranges in C++ - GeeksforGeeks

25 Jul, 2017
std::swap is used for swapping of elements between two containers. One of its variation is std::swap_ranges, which as the name suggests is used for swapping the elements within a range.
It simply exchanges the values of each of the elements in the range [first1, last1) with those of their respective elements in the range beginning at first2. If we look at its internal working, we will find that this function itself uses std::swap().
Syntax:
std::swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
                  ForwardIterator2 first2);

Here, first1, last1 and first2 are forward iterators.
Returns: It returns an iterator to the last element swapped in the second sequence.

// C++ program to demonstrate // the use of std::swap_ranges#include <iostream>#include <algorithm>#include <vector>using namespace std;int main(){    // Declaring first vector    vector<int> v1;    int i;          // v1 contains 0 1 2 3 4 5 6 7 8 9    for (i = 0; i < 10; ++i)     {        v1.push_back(i);    }            // Declaring second vector    // v2 contains 100 100 100 100 100    vector<int> v2(5, 100);          // Performing swap    std::swap_ranges(v1.begin() + 3, v1.begin() + 7, v2.begin());      // Displaying v1 after swapping    for (i = 0; i < 10; ++i)     {        cout << v1[i] << " ";    }      cout << "\n";          // Displaying v2 after swapping    for (i = 0; i < 5; ++i)     {        cout << v2[i] << " ";    }      return 0;}
Output:
0 1 2 100 100 100 100 7 8 9
3 4 5 6 100

Here, in this program we have swapped elements from v1 starting at v1.begin()+3 till v1.begin()+7, with the values starting from v2.begin(), so in place of swapping the whole vector, we have performed swapping in a range.
Where can it be used ?It can be used when we have to find whether a given container contains the same element in its first half as well as in the second half as well, i.e., whether both the halves are identical to each other or not.
// C++ program to demonstrate // the use of std::swap_ranges#include <iostream>#include <algorithm>#include <vector>using namespace std;int main(){    // Here 5 is the central element and the two halves    // on its either side contain the same elements 1 2 3 4    vector<int> v1 = { 1, 2, 3, 4, 5, 1, 2, 3, 4 };      int i;      // Declaring second vector and making it equal to v1    vector<int> v2 = v1;      // Here there is no central element and the two halves    // are 1 2 3 4 and 1 2 3 5 which are different    vector<int> v3 = { 1, 2, 3, 4, 1, 2, 3, 5 };            // Declaring fourth vector and making it equal to v3    vector<int> v4 = v3;      // Performing swap between two halves of vector v1    if (v1.size() % 2 == 0)        std::swap_ranges(v1.begin(), v1.begin() + (v1.size() / 2),                        v1.begin() + v1.size() / 2);      else        std::swap_ranges(v1.begin(), v1.begin() + v1.size() / 2,                        v1.begin() + (v1.size() / 2) + 1);      if (v1 == v2)     {        cout << "Yes";    } else     {        cout << "No";    }      // Now, Performing swap between two halves of vector v3    if (v3.size() % 2 == 0)        std::swap_ranges(v3.begin(), v3.begin() + (v3.size() / 2),                        v3.begin() + v3.size() / 2);      else        std::swap_ranges(v3.begin(), v3.begin() + v3.size() / 2,                        v3.begin() + (v3.size() / 2) + 1);      cout << "\n";      if (v3 == v4)     {        cout << "Yes";    } else     {        cout << "No";    }      return 0;}
Output:
Yes
No

Explanation of code: Here, in this code, we have declared a vector and then assigned it to another vector, and then in the first vector, we are swapping values in the first half with the second half depending upon whether it contains odd no. of elements or not. If after swapping the values, the new first vector is same as second vector (old first vector), this means that both the halves are same.
So, in the first vector 1 2 3 4 5 1 2 3 4, it contains same element in both the halves, so Yes is printed, whereas in second vector 1 2 3 4 1 2 3 5, second half contains one different element than first half, i.e., 4 in place of 5, so No is printed.
Time Complexity: It is linear in the distance between first and last.
This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
cpp-algorithm-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++