(C++) std::swap\_ranges

August 30, 2019 · View on GitHub

std::swap_ranges is an algorithm to swap two container ranges.

 

std::swap_ranges is defined in the header file algorithm.h.

 

In the example below, two ranges in one same container are swapped. The two ranges, however, may also be of two different containers. It is undefined what happens if the ranges overlap.

 


#include <algorithm> #include <iostream> #include <string> int main() {   std::string s = "-AA-BB-";   std::cout << s << '\n';   std::swap_ranges(s.begin()+1,s.begin()+3,s.begin()+4);   std::cout << s << '\n'; }

 

Screen output:

 


-AA-BB- -BB-AA-