Javatpoint Logo
Javatpoint Logo

match_results empty() in C++ STL

Powerful string manipulation utilities are available in C++ Standard Template Library STL. Among these, the std::match_results clause is very important when handling regular expressions. In this blog post, we will delve into the empty() member function of std::match_results and investigate its linguistic expressions, functions, and cases of applications with results.

Before diving into std::match_results understanding what std match results is and its significance for C++ programming will be discussed briefly.

A class template, std::match_results, includes the results of a std::regex_match or std::regex_search match operation. It is just like a box that keeps the matched things inside it that take place in a targeted sequence following a specific expression standard.

Syntax of std::match_results::empty()

The empty() function is a member of the std::match_results class. Its syntax is as follows:

empty(): The fourth member function determines if the std::match_results is empty.

const noexcept: These modification show that the function does not change the object (const) and does not generate exceptions (noexcept).

Understanding std::match_results::empty()

The primary purpose of empty() is to determine whether a std::match_results object would either contain matching results or not possess any possible matches at all. For example, it is very useful when checking if there are any matches of some regular expression pattern in any given input string.

Code:

Let's take an example to understand the use of std::match_results() function in C++:

Output:

Matches found! Results are not empty.
match_results is not empty.

Explanation:

Regular Expression Pattern Definition:

The code begins by defining a regular expression pattern using std::regex. The pattern matches one or more digits (\\d+).

Input String Definition:

Declaration and initialization of a sample input string. It contains a mixture of text and numeric values: Forty-two dollars and ninety-nine cents.

std::match_results Object Declaration:

An instance of the std::match_results class is defined with the iterator typed std::string::const_iterator. In this object, they will be stored after performing the regex search.

Regex Search Operation:

The std::regex_search is used to search for matches on the input string, matching the regular expression pattern. It is where the results are stored under the matches object.

Checking for Matches:

We define a boolean variable hasMatches, which states whether this matches or not. Thereafter, a conditional message is printed depending on the outcome of the search.

Checking if match_results is Empty:

It involves calling a std::match_results::empty() function, which tests whether the matches object is empty. After that, convert the given sentence from ai written to human written. It is possible to print a message using a second conditional statement depending on whether the match_results are empty.

Output Messages:

The program outputs messages to the console indicating whether matches were found and whether the std::empty or null:match_results object.

Return Statement:

The main function terminates when it returns and states that the program has been successfully run.

Conclusion:

In conclusion, the provided C++ code effectively demonstrates the usage of std::Match_empty in regular expression matching. The code sets up a regular expression pattern to find numeric values within a given string, utilizing std::regex_search. The subsequent evaluation of match results and the application of std::match_results::isEmpty() give an unequivocal picture of whether any matches were returned. The reason for this lies in handling and verifying match results in the sense that using regex in C++. It allows programmers to manage the string operations along with the regular expressions in an effective manner.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA