Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::swap_ranges

 

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-

 

 

 

 

 

External links

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict