C++ STL 中 match_results size()


在本文中,我們將討論 C++ STL 中 match_results::size() 函式的工作原理、語法和示例。

C++ STL 中的 match_results 是什麼?

std::match_results 是一種專門的類似於容器的類,用於儲存匹配的字元序列的集合。在這個容器類中,regex 匹配操作查詢目標序列的匹配項。

match_results::size() 是什麼?

match_results::size() 函式是 C++ STL 中的一個內建函式,定義在 <regex> 標頭檔案中。size() 用於獲取與之關聯的 match_results 物件的匹配項數量。此函式返回一個 size_type 值,該值是物件中關聯的函式的匹配項和子匹配項的數量。

語法

smatch_name.size();

引數

此函式不接受任何引數。

返回值

此函式返回 size_type size 或 match_results 物件的匹配項和子匹配項的數量。

示例

Input: string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat;
   regex_match(str, Mat, R);
   Mat.size();
Output: 3

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat;
   regex_match(str, Mat, R);
   cout<<"Size is: " << Mat.size() << endl;
   return 0;
}

輸出

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

Size is: 3

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point Tutorials";
   regex R("(Tutorials)(.*)");
   smatch Mat;
   regex_match(str, Mat, R);
   for (int i = 0; i < Mat.size(); i++) {
      cout <<"length of "<<Mat.length(i)<< endl;
   }
   return 0;
}

輸出

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

length of 25
length of 9
length of 16

更新於: 2020-03-23

93 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.