public member function
<regex>

std::sub_match::compare

int compare (const sub_match& s) const;int compare (const value_type* s) const;int compare (const string_type& s) const;
Compare match
Compares the sequence in the sub_match object against s.

The comparison uses the compare member function of a string object created on the fly for this comparison with the contents of the match.

The function returns 0 if all the characters in the compared contents are equal, a negative value if the first character that does not match compares to less in the object than in s, and a positive value in the opposite case.

sub_match objects can also be compared directly with the set of relational operator overloads.

Parameters

s
A sequence of characters to which the sub_match referred content is compared.

value_type is the character type used in the sequence referred by the iterator type used as template argument (BidirectionalIterator).

string_type is a member type, defined as an alias of the basic_string type corresponding to value_type. I.e., string for all objects whose value_type is char (like csub_match and ssub_match).

Return value

0 if the compared characters sequences are equal, otherwise a number different from 0 is returned, with its sign indicating whether the object is considered greater than s (positive sign), or smaller (negative sign).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// sub_match::compare
#include <iostream>
#include <regex>

int main ()
{
  using namespace std::regex_constants;

  std::cmatch m;

  std::regex_search ( "there is a needle in this haystack", m, std::regex("n\\w+") );

  if (m[0].compare("needle") == 0) std::cout << "needle found!" << std::endl;

  return 0;
}

Output:
needle found!


See also