C++ STL 中的 match_results cbegin() 和 cend()
在本文中,我們將討論 C++ STL 中 match_results::cbegin() 和 match_results::cend() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的 match_results?
std::match_results 是一個專門的類似容器的類,用於儲存匹配到的字元序列的集合。在這個容器類中,正則表示式匹配操作會找到目標序列的匹配項。
什麼是 match_results::cbegin()?
match_results::cbegin() 函式是 C++ STL 中的一個內建函式,它在 <regex> 標頭檔案中定義。此函式返回一個常量迭代器,該迭代器指向 match_results 容器中的第一個元素。常量迭代器不能用於修改容器,常量迭代器僅用於遍歷容器。
語法
smatch_name.cbegin();
引數
此函式不接受任何引數。
返回值
此函式返回一個常量迭代器,該迭代器指向 match_results 容器的第一個元素。
示例
Input: std::string str("TutorialsPoint");
std::smatch Mat;
std::regex re("(Tutorials)(.*)");
std::regex_match ( str, Mat, re );
Mat.cbegin();
Output: T
cbegin()示例
#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 中的一個內建函式,它在 <regex> 標頭檔案中定義。此函式返回一個常量迭代器,該迭代器指向 match_results 容器中最後一個元素的下一個元素。此函式的工作方式與 match_results::end() 相同。
語法
smatch_name.begin();
引數
此函式不接受任何引數。
返回值
此函式返回一個常量迭代器,該迭代器指向 match_results 容器的最後一個元素之後的位置。
Input: std::string str("TutorialsPoint");
std::smatch Mat;
std::regex re("(Tutorials)(.*)");
std::regex_match ( str, Mat, re );
Mat.cend();
Output: m //random value which is past to last.
cend()示例
#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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP