std::swap_ranges in C++

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.
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.