C++ algorithm swap_ranges() function
Example
Swap two ranges of a vector:
vector<char> values = {'1', '2', '3', '4', 'w', 'x', 'y', 'z'};
swap_ranges(values.begin() + 1, values.begin() + 3, values.begin() + 5);
for (char item : values) {
cout << item << " ";
}
Try it Yourself »
Definition and Usage
The swap_ranges()
function swaps the values of a data range with the values in a different data range of the same size.
The data ranges are specified by iterators.
Syntax
swap_ranges(iterator start1, iterator end1, iterator start2);
Parameter Values
Parameter | Description |
---|---|
start1 | Required. An iterator pointing to the start of the first data range. |
end1 | Required. An iterator pointing to the end of the first data range. Elements up to this position will be included, but the element at this position will not be. |
other | Required. An iterator pointing to the start of the second data range. |
Technical Details
Returns: | An iterator pointing to the end of the second data range. |
---|
Related Pages
Read more about data structures in our Data Structures Tutorial.
Read more about iterators in our Iterators Tutorial.
Read more about algorithms in our Algorithms Tutorial.