ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน match_results::prefix() และ match_results::suffix() ใน C++ STL
match_results ใน C++ STL คืออะไร?
std::match_results เป็นคลาสคล้ายคอนเทนเนอร์เฉพาะทาง ที่ใช้เก็บคอลเลกชันของลำดับตัวอักษรที่จับคู่ได้ ในคลาสนี้ regex match operation หา matches ของ target sequence
match_results::prefix() คืออะไร?
ฟังก์ชัน match_results::prefix() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <regex> ใช้หา match_results ก่อนหน้าของ object ที่เกี่ยวข้อง คืนค่า reference ของลำดับที่ตามหลัง match_results object
ไวยากรณ์
match_results.prefix();
พารามิเตอร์
ฟังก์ชันไม่รับพารามิเตอร์
ค่าที่คืน
คืนค่า constant reference ของ string หรือลำดับก่อน match sequence
ตัวอย่าง
Input: string str = "Tutorials Points"; regex R("Points"); smatch Mat; regex_search(str, Mat, R); Mat.prefix(); Output: Tutorials
ตัวอย่างโค้ด prefix()
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Points";
regex R("Points");
smatch Mat;
regex_search(str, Mat, R);
cout << "String prefix is : ";
if(!Mat.empty()) {
cout << Mat.prefix();
}
return 0;
}
เอาต์พุต
String prefix is : Tutorials
match_results::suffix() คืออะไร?
ฟังก์ชัน match_results::suffix() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <regex> ใช้หา match_results ถัดไปของ object ที่เกี่ยวข้อง คืนค่า reference ของลำดับที่ตามหลัง match_results object
ไวยากรณ์
match_results.suffix();
พารามิเตอร์
ฟังก์ชันไม่รับพารามิเตอร์
ค่าที่คืน
คืนค่า constant reference ของ string หรือลำดับหลัง match sequence
ตัวอย่าง
Input: string str("Tutorials Points is the best"); smatch Mat; regex re("Points"); regex_match(str, Mat, re); Mat.suffix(); Output: is the best
ตัวอย่างโค้ด suffix()
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Points is the best";
regex R("Points");
smatch Mat;
regex_search(str, Mat, R);
cout << "String suffix is : ";
if(!Mat.empty()) {
cout << Mat.suffix();
}
return 0;
}
เอาต์พุต
String suffix is : is the best