• Related Questions & Answers

match_results cbegin() add cend() in C++ STL

C++Server Side ProgrammingProgramming

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

match_results::cbegin() function is an inbuilt function in C++ STL, which is defined in <regex> header file. This function returns the constant iterator which is pointing to the first element in the match_results container. Constant iterator can’t be used to do modifications in the container, constant iterator is used for just iterating through the container.

Syntax

smatch_name.cbegin();

Parameters

This function accepts no parameter.

Return value

This function returns constant 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.cbegin();
Output: T
cbegin()

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.cbegin(); i!= Mat.cend(); ++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::cend()?

match_results::cend() function is an inbuilt function in C++ STL, which is defined in <regex> header file. This function returns a constant iterator which is point towards the element next to the last element of match_results container. This function works the same as the match_results::end().

Syntax

smatch_name.begin();

Parameters

This function accepts no parameter.

Return value

This function returns a constant iterator which is pointing to the past the last element of the match_results container.

Input: std::string str("TutorialsPoint");
   std::smatch Mat;
   std::regex re("(Tutorials)(.*)");
   std::regex_match ( str, Mat, re );
   Mat.cend();
Output: m //random value which is past to last.
cend()

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.cbegin(); i!= Mat.cend(); ++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:25:43
Advertisements