match_results empty() in C++ STL


In this article we will be discussing the working, syntax and examples of match_results::empty() function in C++ STL.

What is a match_results in C++ STL?

std::match_results is a specialized container-like class which is used to hold the collection of character sequences which are matched. In this container class a regex match operation finds the matches of the target sequence.

What is match_results::empty()?

match_results::empty() function is an inbuilt function in C++ STL, which is defined in <regex> header file. empty() checks whether the smatch object which is associated is empty or having some match values in it. empty() returns true if the match object is empty or having no matches, if the container is having some values then the function will return false.

Syntax

smatch_name.empty();

Parameters

This function accepts no parameter.

Return value

This function returns Boolean value true if the match object is empty, or there are no matches in the container, else returns false if the match object are having some values or there are some matches available.

Example

Input: std::smatch;
   smatch.empty();
Output: true

Example

 Live Demo

#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;
}

Output

If we run the above code it will generate the following output −

String doesn't matches with Regex-1
String matches with Regex-1

Updated on: 23-Mar-2020

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements