Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ string::compare and string comparison

Compare std::string values with ==, <, <=> and compare(): lexicographic order, the sign of the result, partial comparison, case-insensitive checks and pitfalls.

Strings compare lexicographically: character by character from the start, using the numeric value of each char. The first difference decides; if one string is a prefix of the other, the shorter one is smaller. You normally use the operators ==, !=, <, >, <=, >= (and <=> since C++20). The compare member returns an int and can also compare just a part of a string.

The comparison is by character code, not by alphabet, language or locale: with ASCII all uppercase letters sort before all lowercase letters, so "Zebra" < "apple" is true.

C++
std::string a = "apple", b = "banana";
a < b;            // true
a.compare(b);     // negative
a == "apple";     // true

Try it

C++ Operators and compare

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Case-insensitive and sorted strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
int compare(const basic_string& str) const noexcept;
int compare(size_type pos1, size_type count1, const basic_string& str) const;
int compare(size_type pos1, size_type count1, const basic_string& str,
            size_type pos2, size_type count2 = npos) const;
int compare(const char* s) const;
int compare(size_type pos1, size_type count1, const char* s) const;

bool operator==(const basic_string& lhs, const basic_string& rhs) noexcept;
auto operator<=>(const basic_string& lhs, const basic_string& rhs) noexcept;   // C++20
Name Description
str, s The other string or C string.
pos1, count1 The slice of *this to compare (clamped to the end).
pos2, count2 The slice of str to compare.

Returns: a negative value if *this (or the slice) is smaller, zero if equal, a positive value if larger. The magnitude is unspecified: never compare it to -1 or 1.

Errors: std::out_of_range if pos1 > size() or pos2 > str.size().

Usage notes

  • Use operators for plain checks. a == b is fast: strings of different length are unequal without looking at characters.
  • compare for slices: s.compare(0, 3, "abc") avoids creating a temporary substr.
  • Character values: char may be signed, but the comparison treats characters as unsigned char, so UTF-8 bytes above 127 sort after ASCII.
  • Not locale-aware. For dictionary order in a language, use std::collate or an ICU-based library; byte order is not alphabetical for accented letters.
  • No built-in case-insensitive compare. Write one with tolower on unsigned char (above) or use a case-folding library for Unicode.
  • std::string == const char* compares text. Two const char* values compare addresses, which is almost never what you want.
  • C++20: <=> generates <, <=, > and >=, and != is derived from ==. The result type is std::strong_ordering.
  • Heterogeneous comparison with std::string_view works directly: s == sv.