match_results operator[] 在 C++ STL 中
在本文中,我们将讨论 C++ STL 中 match_results 运算符“[]”的工作原理、语法和示例。
什么是 C++ STL 中的 match_results?
std::match_results 是一个专门的类容器,用于保存已匹配的一系列字符序列。在此容器类中,正则表达式匹配操作可以找到目标序列的匹配项。
什么是 match_results 运算符“[]”
Match_results 运算符[] 是一个引用运算符,用于直接引用 match_results 的第 i 个位置。运算符 [] 返回所关联对象的第 i 个匹配位置。当我们必须通过从零开始的匹配位置直接访问元素时,此运算符会非常有用。
语法
match_results1[int i];
参数
此运算符采用一个整数类型的参数,即我们要访问的元素。
返回值
此函数返回对匹配结果第 i 位置的引用。
示例
Input: string str = "TutorialsPoint";
regex R("(Tutorials)(.*)");
smatch Mat;
regex_match(str, Mat, R);
Mat[0];
Output: TutorialsPoint示例
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "TutorialsPoint";
regex R("(Tutorials)(.*)");
smatch Mat;
regex_match(str, Mat, R);
for (int i = 0; i < Mat.size(); i++) {
cout<<"Match is : " << Mat[i]<< endl;
}
return 0;
}输出
如果我们运行以上代码,它将生成以下输出 −
Match is : TutorialsPoint Match is : Tutorials Match is : Point
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point";
regex R("(Tutorials)(Point)(.*)");
smatch Mat;
regex_match(str, Mat, R);
int len = 0;
string S;
for(int i = 1; i < Mat.size(); i++) {
if (Mat.length(i) > len) {
str = Mat[i];
len = Mat.length(i);
}
}
cout<<"Matching length is : " << len<< endl;
return 0;
}输出
如果我们运行以上代码,它将生成以下输出 −
Matching length is : 0
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP