C++ std::string compare() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::compare() compares this string with another string (or a piece of one) in dictionary order.

Return value is like C strcmp: negative, zero, or positive. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Compare full strings or slices.

C++
int c = a.compare(b);
int d = a.compare(0, 3, b);
CallWhat it does
a.compare(b)Compare all of a with all of b.
a.compare(pos, len, b)Compare a slice of a with b.

For simple equality, prefer a == b. Use compare when you need ordering or substring compares.

Library declarations
C++
int compare(const string& str) const noexcept;
int compare(size_type pos, size_type len, const string& str) const;

Parameters

ParameterDescription
other / slice argsThe string or substring to compare against.

Return Value

Return ValueDescription
int0 if equal; <0 if *this is less; >0 if greater.

C++ Examples

Example 1 — Basic compare()

Compare two equal words.

C++
#include <iostream>
#include <string>

int main() {
    std::string a = "cat";
    std::string b = "cat";
    std::cout << a.compare(b) << '\\n';
    return 0;
}

How It Works

  1. Both strings hold the same characters.
  2. compare returns 0.
  3. That means equality.

Example 2 — Less and Greater

Show negative and positive results.

C++
#include <iostream>
#include <string>

int main() {
    std::string a = "apple";
    std::string b = "banana";
    std::cout << (a.compare(b) < 0 ? "a<b" : "a>=b") << '\\n';
    std::cout << (b.compare(a) > 0 ? "b>a" : "b<=a") << '\\n';
    return 0;
}

How It Works

  1. "apple" comes before "banana".
  2. First compare is negative.
  3. Swapped compare is positive.

Example 3 — Compare a Prefix

Compare only the first three characters.

C++
#include <iostream>
#include <string>

int main() {
    std::string a = "CodeToFun";
    std::string b = "Code";
    std::cout << a.compare(0, 4, b) << '\\n';
    return 0;
}

How It Works

  1. Slice [0,4) of a is "Code".
  2. That equals b.
  3. compare returns 0.

Frequently Asked Questions

0 if equal, negative if *this is less, positive if greater.
Use == / != for equality. Use compare when you need ordering or substring comparison.
Yes for normal char traits.
Yes—overloads accept const char*.
compare is not locale-aware. See strcoll for C-string locale compare.

Next: contains()

Keep going through the string functions. Return true if the string contains a substring or character (C++20).

contains() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful