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
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短