Export (0) Print
Expand All

sub_match::compare

Compare submatch against a sequence.

int compare(const sub_match& right) const;
int compare(const basic_string<value_type>& str) const;
int compare(const value_type *ptr) const;

right

The submatch to compare to.

str

The string to compare to.

ptr

The nul-terminated sequence to compare to.

The first member function compares the matched sequence [first, second) to the matched sequence [right.first, right.second). The second member function compares the matched sequence [first, second) to the character sequence [right.begin(), right.end()). The third member function compares the matched sequence [first, second) to the character sequence [right, right + std::char_traits<value_type>::length(right)).

Each function returns:

a negative value if the first differing value in the matched sequence compares less than the corresponding element in the operand sequence (as determined by std::char_traits<value_type>::compare), or if the two have a common prefix but the target sequence is longer

zero if the two compare equal element by element and have the same length

a positive value otherwise

 

// std_tr1__regex__sub_match_compare.cpp 
// compile with: /EHsc 
#include <regex> 
#include <iostream> 
 
int main() 
    { 
    std::regex rx("c(a*)|(b)"); 
    std::cmatch mr; 
 
    std::regex_search("xcaaay", mr, rx); 
 
    std::csub_match sub = mr[1]; 
    std::cout << "matched == " << std::boolalpha 
        << sub.matched << std::endl; 
    std::cout << "length == " << sub.length() << std::endl; 
 
    std::csub_match::difference_type dif = std::distance(sub.first, sub.second); 
    std::cout << "difference == " << dif << std::endl; 
 
    std::csub_match::iterator first = sub.first; 
    std::csub_match::iterator last = sub.second; 
    std::cout << "range == " << std::string(first, last) 
        << std::endl; 
    std::cout << "string == " << sub << std::endl; 
 
    std::csub_match::value_type *ptr = "aab"; 
    std::cout << "compare(\"aab\") == " 
        << sub.compare(ptr) << std::endl; 
    std::cout << "compare(string) == " 
        << sub.compare(std::string("AAA")) << std::endl; 
    std::cout << "compare(sub) == " 
        << sub.compare(sub) << std::endl; 
 
    return (0); 
    } 
 
matched == true
length == 3
difference == 3
range == aaa
string == aaa
compare("aab") == -1
compare(string) == 1
compare(sub) == 0

Header: <regex>

Namespace: std

Show:
© 2014 Microsoft