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 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短