public member function
<string>
std::string::compare string (1)int compare (const string& str) const;substrings (2)
int compare (size_t pos, size_t len, const string& str) const;int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;c-string (3)
int compare (const char* s) const;int compare (size_t pos, size_t len, const char* s) const;buffer (4)
int compare (size_t pos, size_t len, const char* s, size_t n) const;string (1)
int compare (const string& str) const noexcept;substrings (2)
int compare (size_t pos, size_t len, const string& str) const;int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;c-string (3)
int compare (const char* s) const;int compare (size_t pos, size_t len, const char* s) const;buffer (4)
int compare (size_t pos, size_t len, const char* s, size_t n) const;string (1)
int compare (const string& str) const noexcept;substrings (2)
int compare (size_t pos, size_t len, const string& str) const;int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen = npos) const;c-string (3)
int compare (const char* s) const;int compare (size_t pos, size_t len, const char* s) const;buffer (4)
int compare (size_t pos, size_t len, const char* s, size_t n) const;
Compare strings
Compares the value of the string object (or a substring) to the sequence of characters specified by its arguments.The compared string is the value of the string object or -if the signature used has a pos and a len parameters- the substring that begins at its character in position pos and spans len characters.
This string is compared to a comparing string, which is determined by the other arguments passed to the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// comparing apples with apples
#include <iostream>
#include <string>
int main ()
{
std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
std::cout << str1 << " is not " << str2 << '\n';
if (str1.compare(6,5,"apple") == 0)
std::cout << "still, " << str1 << " is an apple\n";
if (str2.compare(str2.size()-5,5,"apple") == 0)
std::cout << "and " << str2 << " is also an apple\n";
if (str1.compare(6,5,str2,4,5) == 0)
std::cout << "therefore, both are apples\n";
return 0;
}
green apple is not red apple still, green apple is an apple and red apple is also an apple therefore, both are apples
If s does not point to an array long enough, it causes undefined behavior.
If pos is greater than the string length, or if subpos is greater than str's length, an out_of_range exception is thrown.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4