ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน match_results::size() ใน C++ STL
match_results ใน C++ STL คืออะไร?
std::match_results เป็นคลาสคล้ายคอนเทนเนอร์เฉพาะทาง ที่ใช้เก็บคอลเลกชันของลำดับตัวอักษรที่จับคู่ได้ (matched) ในคลาสนี้ regex match operation หา matches ของ target sequence
match_results::size() คืออะไร?
ฟังก์ชัน match_results::size() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <regex> ใช้หาจำนวน matches ของ match_results object
คืนค่า size_type คือจำนวน matches และ sub-matches ใน object
ไวยากรณ์
smatch_name.size();
พารามิเตอร์
ฟังก์ชันไม่รับพารามิเตอร์
ค่าที่คืน
คืนค่า size_type คือจำนวน matches และ sub-matches
ตัวอย่าง
Input: string str = "Tutorials Point"; regex R("(Tutorials)(.*)"); smatch Mat; regex_match(str, Mat, R); Mat.size(); Output: 3
ตัวอย่างโค้ด 1
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point";
regex R("(Tutorials)(.*)");
smatch Mat;
regex_match(str, Mat, R);
cout << "Size is: " << Mat.size() << endl;
return 0;
}
เอาต์พุต
Size is: 3
ตัวอย่างโค้ด 2
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point 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