<regex>

public member function
<regex>

std::match_results::swap

void swap(match_results& mrs);
Swap contents
Exchanges the contents in the match_results object by those of mrs, which is another match_results of the same type. Sizes may differ.

After the call to this member function, the matches in this object are those which were in mrs before the call, and the elements of mrs are those which were in this.

This function exchanges internal pointers to data between the objects without actually performing any copies or moves on the individual matches contained in them, allowing for constant time execution no matter the sizes.

Notice that a global algorithm function exists with this same name, swap. This global function is overloaded for arguments of type match_results to have the same behavior as this member function.

Parameters

mrs
Another match_results object of the same type as this.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// match_results::swap
// - using smatch, a standard alias of match_results<string::iterator>
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("There is a needle in this haystack.");
  std::smatch m1,m2;

  std::regex_search ( s, m1, std::regex("needle") );
  std::regex_search ( s, m2, std::regex("haystack") );

  m1.swap(m2);

  std::cout << m1.format("m1 contains [$0].") << std::endl;
  std::cout << m2.format("m2 contains [$0].") << std::endl;

  return 0;
}


Output:
m1 contains [haystack].
m2 contains [needle].

See also