match_results begin() and end() function in C++ STL
match_results::cbegin() is an inbuilt function in C++ STL that returns an iterator that points to the first match in the match_results object.
Syntax:
smatch_name.begin()
Parameters: This function does not accept any parameters.
Return value: This function returns an iterator pointing to the first match in the match_results object. The matches contained in the match_results object are always constant.
Note: The first element always contains the whole regex match while the others contain the particular Capturing Group.
Below program illustrate the above function:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
string sp( "geeksforgeeks" );
regex re( "(geeks)(.*)" );
smatch match;
std::regex_match(sp, match, re);
cout << "Match size = " << match.size() << endl;
cout << "matches:" << endl;
for (smatch::iterator it = match.begin(); it != match.end(); ++it)
cout << *it << endl;
return 0;
}
|
Output:
Match size = 3
matches:
geeksforgeeks
geeks
forgeeks
match_results::end() is an inbuilt function in C++ STL that returns an iterator that points to the past-the-end match in the match_results object.
Syntax:
smatch_name.end()
Parameters: This function does not accept any parameters.
Return value: This function returns an iterator pointing to the past-the-end match in the match_results object. The matches contained in the match_results object are always constant.
Note: First element always contains the whole regex match while the others contain the particular Capturing Group.
Below program illustrate the above function
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
string sp( "matchresult" );
regex re( "(match)(.*)" );
smatch match;
std::regex_match(sp, match, re);
cout << "Match size = " << match.size() << endl;
cout << "matches:" << endl;
for (smatch::iterator it = match.begin(); it != match.end(); ++it)
cout << *it << endl;
return 0;
}
|
Output:
Match size = 3
matches:
matchresult
match
result
Let us see the differences in a tabular form as follows:
match_results begin() |
match_results end() |
It is used to return an iterator pointing to the first match in the match_results object. |
It is used to return an iterator pointing to the past-the-end match in the match_results object. |
Its syntax is -:
const_iterator begin() const;
|
Its syntax is -:
const_iterator end() const;
|
It does not take any parameters. |
It does not take any parameters. |
Its complexity is constant. |
Its complexity is constant. |
Its iterator validity does not change. |
Its iterator validity does not change. |
Last Updated :
17 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...