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.