function template
<regex>

std::swap (match_results)

template <class BidirectionalIterator, class Alloc>  void swap ( match_results<BidirectionalIterator,Alloc>& lhs,              match_results<BidirectionalIterator,Alloc>& lhs);
交換兩個 match_result 物件的內容
lhs 的內容與 rhs 的內容進行交換。 兩個容器物件必須是相同的型別(相同的模板引數),儘管大小可能不同。

呼叫此成員函式後,lhs 中的匹配項是呼叫之前 rhs 中的匹配項,而 rhs 的匹配項是 lhs 中的匹配項。

這是通用演算法 swap 的一個特化,它透過交換指向資料的內部指標來提高其效能,而無需實際對單個元素執行任何複製或移動操作。

引數

lhs,rhs
match_results 容器(分別在運算子的左側和右側),兩者都具有相同的模板引數 (BidirectionalIteratorAlloc).

返回值



示例

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;
}

可能的輸出
m1 contains [haystack].
m2 contains [needle].


另見