ในบทความนี้เราจะพูดถึงการทำงาน ไวยากรณ์และตัวอย่างของฟังก์ชัน match_results::prefix() และ match_results::suffix() ใน C++ STL
match_results ใน C++ STL คืออะไร
std::match_results เป็นคลาสที่มีลักษณะเหมือนคอนเทนเนอร์พิเศษ ซึ่งใช้เพื่อเก็บคอลเลกชันของลำดับอักขระที่ตรงกัน ในคลาสคอนเทนเนอร์นี้ การดำเนินการจับคู่ regex จะค้นหารายการที่ตรงกันของลำดับเป้าหมาย
match_results::prefix() คืออะไร
ฟังก์ชัน match_results::prefix() เป็นฟังก์ชัน inbuilt ใน C++ STL ซึ่งกำหนดไว้ในไฟล์ส่วนหัว
ไวยากรณ์
match_results.prefix();
พารามิเตอร์
ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ
คืนค่า
ฟังก์ชันนี้ส่งคืนการอ้างอิงคงที่ของสตริงหรือลำดับก่อนหน้าลำดับการจับคู่
ตัวอย่าง
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() เป็นฟังก์ชัน inbuilt ใน C++ STL ซึ่งกำหนดไว้ในไฟล์ส่วนหัว
ไวยากรณ์
match_results.suffix();
พารามิเตอร์
ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ
คืนค่า
ฟังก์ชันนี้ส่งคืนการอ้างอิงคงที่ของสตริงหรือลำดับที่ต่อจากลำดับการจับคู่
ตัวอย่าง
Input: std::string str("Tutorials Points is the best");
std::smatch Mat;
std::regex re("Points");
std::regex_match ( str, Mat, re );
Mat.suffix();
Output: is the best ต่อท้าย()
ตัวอย่าง
#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 prefix is : ";
if (!Mat.empty()) {
cout << Mat.suffix();
}
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
String prefix is : is the best