C ++ STL中的match_results size()
在本文中,我们将讨论C++STL中match_results::size()函数的工作原理,语法和示例。
什么是C++STL中的match_results?
std::match_results是一个类似于容器的特殊类,用于保存匹配的字符序列的集合。在此容器类中,正则表达式匹配操作可找到目标序列的匹配项。
什么是match_results::size()?
match_results::size()函数是C++STL中的内置函数,在<regex>头文件中定义。size()用于获取与之关联的match_results对象的匹配数。该函数返回一个size_type值,该值是与该函数关联的对象中匹配项和子匹配项的数量。
语法
smatch_name.size();
参数
此函数不接受任何参数。
返回值
该函数返回size_type的大小或match_results对象的匹配项和子匹配项的数量。
示例
Input: string str = "nhooo.com";
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 = "nhooo.com";
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 = "nhooo.com 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