ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน match_results::empty() ใน C++ STL
match_results ใน C++ STL คืออะไร?
std::match_results เป็นคลาสคล้ายคอนเทนเนอร์เฉพาะทาง ที่ใช้เก็บคอลเลกชันของลำดับตัวอักษรที่จับคู่ได้ ในคลาสนี้ regex match operation หา matches ของ target sequence
match_results::empty() คืออะไร?
ฟังก์ชัน match_results::empty() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <regex> ตรวจสอบว่า smatch object ว่างหรือมีค่า match อยู่
คืนค่า true หาก match object ว่างหรือไม่มี matches, false หากมีค่า
ไวยากรณ์
smatch_name.empty();
พารามิเตอร์
ฟังก์ชันไม่รับพารามิเตอร์
ค่าที่คืน
คืนค่า Boolean: true หาก match object ว่าง, false หากมี matches
ตัวอย่าง
Input: std::smatch; smatch.empty(); Output: true
ตัวอย่างโค้ด
#include <bits/stdc++.h>
using namespace std;
int main() {
string str("Tutorials");
regex R_1("Points.*");
regex R_2("Tutorials.*");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R_1);
regex_match(str, Mat_2, R_2);
if(Mat_1.empty()) {
cout << "String doesn't matches with Regex-1" << endl;
} else {
cout << "String matches with Regex-1" << endl;
}
if(Mat_2.empty()) {
cout << "String doesn't matches with Regex-2" << endl;
} else {
cout << "String matches with Regex-2" << endl;
}
return 0;
}
เอาต์พุต
String doesn't matches with Regex-1 String matches with Regex-2