C++ 中的 match_results 运算符 =
在本文中,我们将讨论 C++ STL 中 match_results 运算符 "=" 的作用、语法和示例。
什么是 C++ STL 中的 match_results?
std::match_results 是一个专门的类似容器的类,用于保存匹配的字符序列的集合。在这个容器类中,正则匹配运算会找到目标序列的匹配项。
什么是 match_results 运算符“=”
Match_results 运算符 = 是一个用于为 match_results 赋值的相等运算符。运算符 = 用于从一个 match_results 对象复制或移动元素到另一个 match_results 对象。
语法
match_results1 = (match_results2);
参数
另一个 match_results 对象,我们要将它的数据复制到一个 match_results 对象。
返回值
这不会返回任何值。
示例
Input: string str = "Tutorials Point";
regex R("(Tutorials)(.*)");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R);
Mat_2 = Mat_1;
Output: MAT 2 =
Tutorials Point
Tutorials
Point示例
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point";
regex R("(Tutorials)(.*)");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R);
Mat_2 = Mat_1;
cout<<"String matches: " << endl;
for (smatch::iterator i = Mat_2.begin(); i!= Mat_2.end(); i++) {
cout << *i << endl;
}
}输出
如果我们运行上述代码,它将生成以下输出 −
String matches: Tutorials Point Tutorials Point
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point";
regex R_1("(Tutorials)(.*)");
regex R_2("(Po)(int)(.*)");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R_1);
regex_match(str, Mat_2, R_2);
smatch Mat;
if (Mat_1.size() > Mat_2.size()) {
Mat = Mat_1;
} else {
Mat = Mat_2;
}
cout<<"string matches " << endl;
for (smatch::iterator i = Mat.begin(); i!= Mat.end(); i++) {
cout << *i << endl;
}
}输出
如果我们运行上述代码,它将生成以下输出 −
String matches: Tutorials Point Tutorials Point
赞助商
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP