C++ STL 中 match_results 的 begin() 和 end() 函式
本文將討論 C++ STL 中 match_results::begin() 和 match_results::end() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的 match_results?
std::match_results 是一個專門的類似容器的類,用於儲存匹配到的字元序列的集合。在這個容器類中,正則表示式匹配操作查詢目標序列的匹配項。
什麼是 match_results::begin()?
match_results::begin() 函式是 C++ STL 中的一個內建函式,它在 <regex> 標頭檔案中定義。此函式返回一個迭代器,該迭代器指向 match_results 物件中的第一個元素。match_results::begin() 和 match_results::end() 結合使用以提供 match_results 容器的範圍。
語法
match_name.begin();
引數
此函式不接受任何引數。
返回值
此函式返回一個迭代器,該迭代器指向 match_results 容器的第一個元素。
示例
Input: std::string str("TutorialsPoint"); std::smatch Mat; std::regex re("(Tutorials)(.*)"); std::regex_match ( str, Mat, re ); Mat.begin(); 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.begin(); i!= Mat.end(); ++i) { std::cout << *i << std::endl; } return 0; }
輸出
如果我們執行以上程式碼,它將生成以下輸出:
Match Found Tutorials Tuto rials
什麼是 match_results::end()?
match_results::end() 函式是 C++ STL 中的一個內建函式,它在 <regex> 標頭檔案中定義。此函式返回一個迭代器,該迭代器指向 match_results 物件中最後一個元素的下一個位置。match_results::begin() 和 match_results::end() 結合使用以提供 match_results 容器的範圍。
語法
smatch_name.begin();
引數
此函式不接受任何引數。
返回值
此函式返回一個迭代器,該迭代器指向 match_results 容器末尾的下一個元素。
Input: std::string str("TutorialsPoint"); std::smatch Mat; std::regex re("(Tutorials)(.*)"); std::regex_match ( str, Mat, re ); Mat.end(); Output: m //Random value which is past to the end of the container.
示例
#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.begin(); i!= Mat.end(); ++i) { std::cout << *i << std::endl; } return 0; }
輸出
如果我們執行以上程式碼,它將生成以下輸出:
Match Found Tutorials Tuto rials
廣告