What is the Difference Between C++ String == and compare()? Last Updated : 15 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++ == and compare() both are used to compare strings and find if the given strings are equal or not but they differ in working. In this article, we will learn the key differences between == and compare() of string in C++. "==" Operator in C++The == operator in C++ is used to compare two strings it is a boolean operator that returns true if the given strings are equal and returns false if they are not equal (in case each corresponding character of string or length does not match). Syntax of "==" operator in C++str1 == str2Example of "==" Operator in C++The below example demonstrates the use of the "==" operator to compare the strings. C++ // C++ program to demonstrate the use of == operator to // compare the strings #include <iostream> #include <string> using namespace std; int main() { // defining strings to be compared string s1 = "GfG"; string s2 = "GeeksforGeeks"; // comparing string using == operator if (s1 == s2) { cout << "Strings are Equal\n"; } else { cout << "Strings are not Equal\n"; } return 0; } OutputStrings are not Equal String compare() in C++The std::string::compare() function is also used to compare strings but it returns an integer unlike == operator to tell how the string are related lexicographically. returnValue == 0: Strings are equal.returnValue <0: First string is lexicographically less than the second string.returnValue >0: First string is lexicographically greater than the second string.Syntax of Compare() int returnValue = str1.compare(str2) Example of compare() in C++The below example demonstrates the use compare() function to compare strings in C++. C++ // C++ program to demonstrate the use compare() function to // compare strings #include <iostream> #include <string> using namespace std; int main() { string s1 = "GfG"; string s2 = "GeeksforGeeks"; int value = s1.compare(s2); if (value == 0) { cout << "The strings are Equal\n"; } // value > 0 (s1 is greater than s2) else if (value > 0) { cout << "The string1 is greater than string 2\n"; } // Now the only left value < 0 (s1 is smaller than s2) else { cout << "The string1 is smaller than string 2\n"; } return 0; } OutputThe string1 is greater than string 2 Difference Between C++ String == and Compare()The below table illustrates the key difference between C++ string == and compare(). Feature == Operator compare() Method Usage Check two strings have same content or not Compare two strings Lexicographically Syntax string1 == string2 string1.compare(string2) Return Type Boolean (true or false) Integer (positive,negative or '0') Flexibility Limited to full string comparison Can compare whole strings, substrings, or parts of strings Use Case When we need to check if two strings are identical or not When we need Determines lexical ordering along with equality Comment More infoAdvertise with us Next Article What is the Difference Between C++ String == and compare()? H harshsingh123 Follow Improve Article Tags : C++ Programs C++ cpp-string cpp-operator CPP Examples +1 More Practice Tags : CPPcpp-operator Similar Reads Difference Between string and char[] Types in C++ In C++, we can store the sequence of characters i.e. string in two ways: either as a std::string object or char array. In this article, we will discuss some major differences between string and char[] in C++.Character Array Type StringsA character array is simply an array in C++ that contains charac 2 min read Difference Between Array of Characters and std::string in C++ In C++, we have character array and std::string class both of which are used to store the sequence of characters. The character arrays are a part of C-style programming on the other hand std::string is a part of the C++ standard library. In this article, we will discuss what are some major differenc 3 min read Difference Between std::wstring and std::string The std::wstring and std::string are the classes in C++ used to store sequences of characters. While serving similar purposes, they serve different requirements. In this article, we will look at some major differences between the std::wstring and std::string in C++. Wide String in C++The std::wstrin 3 min read How to Compare Two Substrings in a Character Array in C++? In C++, character arrays are used to store a sequence of characters also known as strings. A Substring is a continuous sequence of characters within a string. In this article, we will learn how we can compare two substrings in a character array in C++. Examples: Input: string: "Hello World" Substrin 2 min read C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad 1 min read How to Check if a String is Empty in C++? In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for a 2 min read How to Compare Two Lists in C++ STL? In C++, lists are containers provided by the STL library of C++, which allows us to store elements of the same data type in non-contiguous memory locations. Comparing two lists is a very common operation while using lists. In this article, we will learn how to compare two lists in C++. Example: Inpu 2 min read Case-Insensitive String Comparison in C++ In C++, strings are sequences of characters that are used to store text data which can be in both uppercase and lowercase format. In this article, we will learn how we can compare two case-insensitive strings in C++. Example: Input: string1 = "WELCOME TO GEEKS FOR GEEKS"string2 = "welcome to geeks f 2 min read 3-way comparison operator (Space Ship Operator) in C++ 20 The three-way comparison operator "<=>" is called a spaceship operator. The spaceship operator determines for two objects A and B whether A < B, A = B, or A > B. The spaceship operator or the compiler can auto-generate it for us. Also, a three-way comparison is a function that will give 3 min read C++ Program to Sort String of Characters Sorting a string means rearranging the characters of the given string in some defined order such as alphabetical order. In this article, we will learn how to sort a string by characters in C++.ExamplesInput: str = "geeksforgeeks"Output: "eeeefggkkorss"Explanation: The characters in the string are so 4 min read Like