match_results begin() and end() function in C++ STL



In this article we will be discussing the working, syntax and examples of match_results::begin() and match_results::end() functions 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::begin()?

match_results::begin() function is an inbuilt function in C++ STL, which is defined in <regex> header file. This function returns an iterator which is pointing to the first element in the match_results object. The match_results::begin() and match_results::end() are together used to give the range of the match_results container.

Syntax

match_name.begin();

Parameters

This function accepts no parameter.

Return value

This function returns an iterator which is pointing to the first element of the match_results container.

Example

Input: std::string str("TutorialsPoint");
std::smatch Mat;
std::regex re("(Tutorials)(.*)");
std::regex_match ( str, Mat, re );
Mat.begin();
Output: T

Example

 Live Demo

#include <iostream>
#include <string>
#include <regex>
int main () {
   std::string str("Tutorials");
   std::smatch Mat;
   std::regex re("(Tuto)(.*)");
   std::regex_match ( str, Mat, re );
   std::cout<<"Match Found: " << std::endl;
   for (std::smatch::iterator i = Mat.begin(); i!= Mat.end(); ++i) {
      std::cout << *i << std::endl;
   }
   return 0;
}

Output

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

Match Found
Tutorials
Tuto
rials

What is match_results::end()?

match_results::end() function is an inbuilt function in C++ STL, which is defined in <regex> header file. This function returns an iterator which is pointing to the position next to the last element in the match_results object. The match_results::begin() and match_results::end() are togetherly used to give the range of the match_results container.

Syntax

smatch_name.begin();

Parameters

This function accepts no parameter.

Return value

This function returns an iterator which is pointing to the element past the end of the match_results container.

Input: std::string str("TutorialsPoint");
std::smatch Mat;
std::regex re("(Tutorials)(.*)");
std::regex_match ( str, Mat, re );
Mat.end();
Output: m //Random value which is past to the end of the container.

Example

 Live Demo

#include <iostream>
#include <string>
#include <regex>
int main () {
   std::string str("Tutorials");
   std::smatch Mat;
   std::regex re("(Tuto)(.*)");
   std::regex_match ( str, Mat, re );
   std::cout<<"Match Found: " << std::endl;
   for (std::smatch::iterator i = Mat.begin(); i!= Mat.end(); ++i) {
      std::cout << *i << std::endl;
   }
   return 0;
}

Output

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

Match Found
Tutorials
Tuto
rials
raja
Published on 23-Mar-2020 06:12:54
Advertisements