Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน match_results::cbegin() และ match_results::cend() ใน C++ STL

ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน match_results::cbegin() และ match_results::cend() ใน C++ STL

match_results ใน C++ STL คืออะไร?

std::match_results เป็นคลาสคล้ายคอนเทนเนอร์เฉพาะทาง ที่ใช้เก็บคอลเลกชันของลำดับตัวอักษรที่จับคู่ได้ ในคลาสนี้ regex match operation หา matches ของ target sequence

match_results::cbegin() คืออะไร?

ฟังก์ชัน match_results::cbegin() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <regex> คืนค่า constant iterator ชี้ไปยังองค์ประกอบแรกใน match_results container

Constant iterator ไม่สามารถแก้ไขคอนเทนเนอร์ได้ ใช้สำหรับ iterate เท่านั้น

ไวยากรณ์

smatch_name.cbegin();

พารามิเตอร์

ฟังก์ชันไม่รับพารามิเตอร์

ค่าที่คืน

คืนค่า constant iterator ชี้ไปยังองค์ประกอบแรกของ match_results

ตัวอย่าง

Input: string str("TutorialsPoint"); smatch Mat; regex re("(Tutorials)(.*)"); regex_match(str, Mat, re); Mat.cbegin(); Output: T

ตัวอย่างโค้ด

#include <iostream>
#include <string>
#include <regex>

int main() {
    std::string str("Tutorials");
    std::smatch Mat;
    std::regex re("(Tuto)(.*)");
    std::regex_match(str, Mat, re);
    std::cout << "Match Found: " << std::endl;
    for(std::smatch::iterator i = Mat.cbegin(); i != Mat.cend(); ++i) {
        std::cout << *i << std::endl;
    }
    return 0;
}

เอาต์พุต

Match Found
Tutorials
Tuto
rials

match_results::cend() คืออะไร?

ฟังก์ชัน match_results::cend() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <regex> คืนค่า constant iterator ชี้ไปยังตำแหน่งหลังองค์ประกอบสุดท้าย ทำงานเหมือน match_results::end()

ไวยากรณ์

smatch_name.cend();

พารามิเตอร์

ฟังก์ชันไม่รับพารามิเตอร์

ค่าที่คืน

คืนค่า constant iterator ชี้ไปยัง past-the-last element ของ match_results

ตัวอย่าง

Input: string str("TutorialsPoint"); smatch Mat; regex re("(Tutorials)(.*)"); regex_match(str, Mat, re); Mat.cend(); Output: m // ค่าสุ่ม past-the-last