C ++ STL中的match_results empty()
在本文中,我们将讨论C++STL中match_results::empty()函数的工作,语法和示例。
什么是C++STL中的match_results?
std::match_results是一个类似于容器的特殊类,用于保存匹配的字符序列的集合。在此容器类中,正则表达式匹配操作可找到目标序列的匹配项。
什么是match_results::empty()?
match_results::empty()函数是C++STL中的内置函数,在<regex>头文件中定义。empty()检查关联的smatch对象是否为空或其中是否包含一些匹配值。empty()如果match对象为空或没有匹配项,则返回true;如果容器具有某些值,则该函数将返回false。
语法
smatch_name.empty();
参数
此函数不接受任何参数。
返回值
如果match对象为空,或者容器中没有匹配项,则此函数返回布尔值true;否则,如果match对象具有某些值或存在某些匹配项,则返回false。
示例
Input: std::smatch; smatch.empty(); Output: true
示例
#include<bits/stdc++.h>
using namespace std;
int main() {
string str("Tutorials");
regex R_1("Points.*");
regex R_2("Tutorials.*");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R_1);
regex_match(str, Mat_2, R_2);
if (Mat_1.empty()) {
cout<<"String doesn't matches with Regex-1" << endl;
} else {
cout << "String matches with Regex-1" << endl;
}
if (Mat_2.empty()) {
cout << "String doesn't matches with Regex-2" << endl;
} else {
cout << "String matches with Regex-1" << endl;
}
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
String doesn't matches with Regex-1 String matches with Regex-1