公共成員函式
<regex>

std::match_results::swap

void swap(match_results& mrs);
交換內容
match_results 物件中的內容與 mrs 的內容交換,後者是另一個相同型別的 match_results。 大小可能不同。

呼叫此成員函式後,此物件中的匹配項是呼叫之前 mrs 中的匹配項,mrs 的元素是此物件.

此函式交換物件之間指向資料的內部指標,而無需實際對其中包含的各個匹配項執行任何複製或移動操作,從而允許恆定時間執行,無論大小如何。

請注意,存在一個具有相同名稱的全域性演算法函式 swap。 此全域性函式針對 match_results 型別的引數過載,以具有與此成員函式相同的行為。

引數

mrs
另一個與此物件相同型別的 match_results 物件。

返回值



示例

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

輸出
m1 contains [haystack].
m2 contains [needle].


另見