在 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

更新於:2020 年 3 月 23 日

70 次瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

立即開始
廣告