ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของตัวดำเนินการ match_results '=' ใน C++ STL
match_results ใน C++ STL คืออะไร
std::match_results เป็นคลาสที่มีลักษณะเหมือนคอนเทนเนอร์พิเศษ ซึ่งใช้เพื่อเก็บคอลเลกชันของลำดับอักขระที่ตรงกัน ในคลาสคอนเทนเนอร์นี้ การดำเนินการจับคู่ regex จะค้นหารายการที่ตรงกันของลำดับเป้าหมาย
ตัวดำเนินการ match_results คืออะไร '='
ตัวดำเนินการ Match_results =เป็นตัวดำเนินการความเท่าเทียมกันที่ใช้ในการกำหนดค่าให้กับ match_results Operator =ใช้เพื่อคัดลอกหรือย้ายองค์ประกอบจากวัตถุ 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