在本文中,我们将讨论C ++ STL中match_results::begin()和match_results::end()函数的工作原理,语法和示例。
std::match_results是一个类似于容器的特殊类,用于保存匹配的字符序列的集合。在此容器类中,正则表达式匹配操作可找到目标序列的匹配项。
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("nhooo");
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()函数是C ++ STL中的内置函数,在<regex>头文件中定义。此函数返回一个迭代器,该迭代器指向match_results对象中最后一个元素旁边的位置。match_results::begin()和match_results::end()一起用于给出match_results容器的范围。
smatch_name.begin();
此函数不接受任何参数。
此函数返回一个迭代器,该迭代器指向比match_results容器结尾更远的元素。
Input: std::string str("nhooo");
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