The implementation of class templates provides great flexibility for the types that can be supported as elements. You can exchange some elements between a vector and a list, swapping specific elements with the third-last values according to the given specifications. Additionally, ranges in two vectors can also be swapped.
What is the std::swap_ranges?
This algorithm swaps the elements in specified ranges between two rows. It requires four arguments: the start and end iterators of the first stage and the start repeater of the second stage. Once the last element is modified, an iterator pointing to the element on the second range is returned.
Template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap ranges(ForwardIterator1 first1, ForwardIterator1 last1, forwardIterator2 first2);
Complexity: The number of variables being swapped is n, and the linear complexity of the std::swap_ranges algorithm is O(n).
Constraints: In order to avoid undefined behavior when using std::swap_ranges, ensure that both containers have a sufficient number of elements in the specified ranges.
Personalization: If you need to modify components according to a custom operation or situation, you can customize it using a loop or std::transform.
Pseudocode:
function swapSubranges(container1, container2, start1, end1, start2, end2):
// Ensure the ranges are valid
if start1 >= end1 or start2 >= end2 or end1 > container1.size() or end2 > container2.size():
return
// Swap the subranges
for i from start1 to end1 - 1:
temp = container1[i]
container1[i] = container2[start2 + i - start1]
container2[start2 + i - start1] = temp
// Example usage
vec1=[1,2,3,4,5]
vec2=[10,20,30,40,50]
swapSubranges(vec1,vec2,1,3,1,4)
Example 1:
Let us take an example to illustrate how to swapping of subranges from different containers in C++.
#include<iostream>
#include<vector>
#include<algorithm>
Int main()
{
std::vector<int>vec1={1,2,3,4,5};
std::vector<int>vec2={10,20,30,40,50};
//swap subrange from vec1[1,3] with subrange from vec2[1,4]
auto first1 = vec1.begin() + 1;
auto last1 = vec1.begin() + 3;
auto first2 = vec2.begin() + 1;
auto last2 = vec2.begin() + 4;
std::swap_ranges(first1, last1, first2);
// Print the results
std::cout << "vec1 after swapping: ";
for (const auto& elem : vec1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "vec2 after swapping: ";
for (const auto& elem : vec2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Example 2:
Let us take another example to illustrate the swap subranges from different containers in C++.
#inlcude<algorithm>
#include<iostream>
#include<list>
#include<vector>
using namespace std;
int main()
{
vector<int>v={-1,-5,-3,2,50};
list<int>lt ={1,5,3,10,5};
vector<int>v={-1,-5,-3,2,50};
list<int>lt={1,5,3,10,5};
/* swap_ranges() swaps the values starting from the beginning
to 3rd last values as per the parameters.
Hence (-10, -15, -30) are swapped with (10, 50, 30). */
swap_ranges(v.begin(), v.begin() + 3, lt.begin());
for (int n: v)
cout << n << ' ';
cout << '\n';
for (int n: lt)
cout << n << ' ';
cout << endl;
}
Output:
Example 3:
Let us take another example to illustrate the swap subranges from different containers in C++.
#include<algorithm>
#include<iostream>
#include<list>
#include<vector>
int main(){
std::vector<int>v1{1,2,3,4,5};
std::list<int>l1{10,20,30,40,50};
//Swapping subranges [1,4] of v1 with [1,4]of l1
auto first1 = v1.begin() + 1;
auto last1 = v1.begin() + 4;
auto first2 = l1.begin();
auto last2 = l1.begin();
std::advance(first2, 1);
std::advance(last2, 4);
std::swap_ranges(first1, last1, first2);
// Printing the results
std::cout << "v1 after swap_ranges:\n";
for (const auto& elem : v1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "l1 after swap_ranges:\n";
for (const auto& elem : l1) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Conclusion:
The std::swap_ranges method in C++ is an efficient way to swap subranges from a separate container. This method is a quick and easy way to exchange material between containers because the material is exchanged at a fixed point in both layers.
In order to prevent undefined behavior when using std::swapranges, ensure the ranges you supply are valid and do not overlap. Also, keep in mind that std::swapranges have linear complexity, making it possible to swap even larger subranges.
Overall, std::swap_ranges provides a powerful tool for organizing and manipulating containers, making it easy to swap subranges between different C++ sequences.