在 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() 返回 true;如果容器中有一些值,则该函数将返回 false。
语法
smatch_name.empty();
参数
该函数不接受任何参数。
返回值
如果匹配对象为空,或容器中没有匹配项,则该函数返回布尔值 true;否则,如果匹配对象中有一些值或存在一些匹配项,则返回 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP