在本文中,我们将讨论C ++ STL中match_results::cbegin()和match_results::cend()函数的工作原理,语法和示例。
std::match_results是一个类似于容器的特殊类,用于保存匹配的字符序列的集合。在此容器类中,正则表达式匹配操作可找到目标序列的匹配项。
match_results::cbegin()函数是C ++ STL中的内置函数,该函数在<regex>头文件中定义。此函数返回常量迭代器,该迭代器指向match_results容器中的第一个元素。常量迭代器不能用于在容器中进行修改,常量迭代器仅用于遍历容器。
smatch_name.cbegin();
此函数不接受任何参数。
此函数返回常量迭代器,该迭代器指向match_results容器的第一个元素。
Input: std::string str("nhooo");
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()函数是C ++ STL中的内置函数,在<regex>头文件中定义。此函数返回一个常量迭代器,该迭代器指向match_results容器的最后一个元素旁边的元素。此功能与match_results::end()相同。
smatch_name.begin();
此函数不接受任何参数。
该函数返回一个常量迭代器,该迭代器指向match_results容器的最后一个元素。
Input: std::string str("nhooo");
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