ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของ match_results operator = ใน C++ STL
match_results ใน C++ STL คืออะไร?
std::match_results เป็นคลาสคล้ายคอนเทนเนอร์เฉพาะทาง ที่ใช้เก็บคอลเลกชันของลำดับตัวอักษรที่จับคู่ได้ ในคลาสนี้ regex match operation หา matches ของ target sequence
match_results operator = คืออะไร?
Match_results operator = เป็น equality operator ใช้กำหนดค่าให้ match_results ใช้ copy หรือ move elements จาก match_results object หนึ่งไปอีกอันหนึ่ง
ไวยากรณ์
match_results1 = (match_results2);
พารามิเตอร์
match_results object อีกอันหนึ่งที่ต้องการ copy ข้อมูลมา
ค่าที่คืน
ไม่คืนค่า (void)
ตัวอย่าง
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
ตัวอย่างโค้ด 1
#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;
}
return 0;
}
เอาต์พุต
String matches: Tutorials Point Tutorials Point
ตัวอย่างโค้ด 2
#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;
}
return 0;
}
เอาต์พุต
String matches: Tutorials Point Tutorials Point