I need some built-in function to compare string
s. For C-style strings I have
strcmp();
But I need some function to deal with the string
class.
string name1;
string name2;
I need some built-in function to compare string
s. For C-style strings I have
strcmp();
But I need some function to deal with the string
class.
string name1;
string name2;
You're looking for the equality operator, operator==()
, which is defined for std::basic_string
:
if (name1 == name2)
Other comparison operators are also available, namely !=
, <
, <=
, >
and >=
.
You could use std::string::compare()
which provides the same functionality as strcmp()
.
std::string name1 = "John";
std::string name2 = "Micheal";
int result = name1.compare(name2);
Would roughly be the same as:
const char* name1 = "John";
const char* name2 = "Micheal";
int result = std::strcmp(name1, name2);