function template
<regex>
std::swap (match_results)
template <class BidirectionalIterator, class Alloc>
void swap ( match_results<BidirectionalIterator,Alloc>& lhs,
match_results<BidirectionalIterator,Alloc>& lhs);
Swap contents of two match_result objects
The contents of lhs are exchanged with those of rhs. Both container objects must be of the same type (same template parameters), although sizes may differ.
After the call to this member function, the matches in lhs are those which were in rhs before the call, and the matches of rhs are those which were in lhs.
This is a specialization of the generic algorithm swap that improves its performance by exchanging internal pointers to data, without actually performing any copies or moves on the individual elements.
Parameters
- lhs,rhs
- match_results containers (to the left- and right-hand side of the operator, respectively), having both the same template parameters (BidirectionalIterator and Alloc).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// swap match_results
// - 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") );
swap(m1,m2);
std::cout << m1.format("m1 contains [$0].") << std::endl;
std::cout << m2.format("m2 contains [$0].") << std::endl;
return 0;
}
| |
Possible output:
m1 contains [haystack].
m2 contains [needle].
|
See also
- match_results::swap
- Swap contents (public member function)
- swap
- Exchange values of two objects (function template
)
- swap_ranges
- Exchange values of two ranges (function template
)