C++ 中的 match_results 運算子 =


在本文中,我們將討論 C++ STL 中 match_results 運算子 "=" 的作用、語法和示例。

什麼是 C++ STL 中的 match_results?

std::match_results 是一個專門的類似容器的類,用於儲存匹配的字元序列的集合。在這個容器類中,正則匹配運算會找到目標序列的匹配項。

什麼是 match_results 運算子“=”

Match_results 運算子 = 是一個用於為 match_results 賦值的相等運算子。運算子 = 用於從一個 match_results 物件複製或移動元素到另一個 match_results 物件。

語法

match_results1 = (match_results2);

引數

另一個 match_results 物件,我們要將它的資料複製到一個 match_results 物件。

返回值

這不會返回任何值。

示例

Input: string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat_1, Mat_2;
   regex_match(str, Mat_1, R);
   Mat_2 = Mat_1;
Output: MAT 2 =
   Tutorials Point
   Tutorials
   Point

示例

 現場演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat_1, Mat_2;
   regex_match(str, Mat_1, R);
   Mat_2 = Mat_1;
   cout<<"String matches: " << endl;
   for (smatch::iterator i = Mat_2.begin(); i!= Mat_2.end(); i++) {
      cout << *i << endl;
   }
}

輸出

如果我們執行上述程式碼,它將生成以下輸出 −

String matches:
Tutorials Point
Tutorials
Point

示例

 現場演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point";
   regex R_1("(Tutorials)(.*)");
   regex R_2("(Po)(int)(.*)");
   smatch Mat_1, Mat_2;
   regex_match(str, Mat_1, R_1);
   regex_match(str, Mat_2, R_2);
   smatch Mat;
   if (Mat_1.size() > Mat_2.size()) {
      Mat = Mat_1;
   } else {
      Mat = Mat_2;
   }
   cout<<"string matches " << endl;
   for (smatch::iterator i = Mat.begin(); i!= Mat.end(); i++) {
      cout << *i << endl;
   }
}

輸出

如果我們執行上述程式碼,它將生成以下輸出 −

String matches:
Tutorials Point
Tutorials
Point

更新於:2020-03-23

102 次瀏覽

開啟你的 職業

完成課程認證

開始學習
贊助商
© . All rights reserved.