Difference between Relational operator(==) and std::string::compare() in C++ Last Updated : 08 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Relational operators vs std::string::compare() Return Value: Relational operators return boolean value, while compare() returns unsigned integer.Parameters : Relational operators need only two strings to perform comparison, one which is being compared and other one is for reference, while compare() can accept different arguments to perform certain task accordingly.Comparison Method : Relational operators compare characters lexicographically according to the current character traits, while compare() can process more than one argument for each string so that you can specify a substring by its index and by its length.Operation : We can perform comparison in a part of string directly, using compare() which is otherwise quite a long process with relational operators. Example : * Compare 3 characters from 3rd position of str1 with 3 characters from 4th position of str2.* str1 = "GeeksforGeeks" * str2 = "HelloWorld!"Using compare() : CPP // CPP code to perform comparison using compare() #include <iostream> using namespace std; void usingCompare(string str1, string str2) { // Direct Comparison if (str1.compare(2, 3, str2, 3, 3) == 0) cout << "Both are same"; else cout << "Not equal"; } // Main function int main() { string s1("GeeksforGeeks"); string s2("HelloWorld !"); usingCompare(s1, s2); return 0; } Output:Not equalUsing Relational Operators : CPP // CPP code for comparison using relational operator #include <iostream> using namespace std; void relational_operation(string s1, string s2) { int i, j; // Lexicographic comparison for (i = 2, j = 3; i <= 5 && j <= 6; i++, j++) { if (s1[i] != s2[j]) break; } if (i == 6 && j == 7) cout << "Equal"; else cout << "Not equal"; } // Main function int main() { string s1("GeeksforGeeks"); string s2("HelloWorld !"); relational_operation(s1, s2); return 0; } Output:Not equalWe can clearly observe the extra processing we need to go through while using relational operators. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. Comment More infoAdvertise with us Next Article Difference between Relational operator(==) and std::string::compare() in C++ S Sakshi Tiwari Improve Article Tags : Misc Difference Between C++ STL cpp-operator cpp-strings-library +2 More Practice Tags : CPPcpp-operatorMiscSTL Similar Reads Comparing String objects using Relational Operators in C++ If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character i 2 min read Difference between strncmp() and strcmp in C/C++ The basic difference between these two are : strcmp compares both the strings till null-character of either string comes whereas strncmp compares at most num characters of both strings. But if num is equal to the length of either string than strncmp behaves similar to strcmp.Problem with strcmp func 3 min read What is the difference between = (Assignment) and == (Equal to) operators = operator The "=" is an assignment operator used to assign the value on the right to the variable on the left. For example: a = 10; b = 20; ch = 'y'; Example: C // C program to demonstrate // working of Assignment operators #include <stdio.h> int main() { // Assigning value 10 to a // using 2 min read Results of comparison operations in C and C++ In C, data type of result of comparison operations is int. For example, see the following program. C #include<stdio.h> int main() { int x = 10, y = 10; printf("%d \n", sizeof(x == y)); printf("%d \n", sizeof(x < y)); return 0; } Output4 4 Whereas in C++, type of results 1 min read Difference Between == Operator and equals() Method in Java In Java, the equals() method and the == operator are used to compare objects. The main difference is that string equals() method compares the content equality of two strings while the == operator compares the reference or memory location of objects in a heap, whether they point to the same location 4 min read What is the difference between == and === in PHP ? In this article, we will discuss the differences between '==' and '===' operators in PHP. Both are comparison operators used to compare two or more values. == Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: oper 2 min read Difference Between Equality of Objects and Equality of References in Java Equality of objects means when two separate objects happen to have the same values/state. Whereas equality of references means when two object references point to the same object. The == operator can be used to check if two object references point to the same object. To be able to compare two java o 2 min read C++ program to compare two Strings using Operator Overloading Pre-requisite: Operator Overloading in C++Given two strings, how to check if the two strings are equal or not, using Operator Overloading. Examples: Input: ABCD, XYZ Output: ABCD is not equal to XYZ ABCD is greater than XYZ Input: Geeks, Geeks Output: Geeks is equal to Geeks Approach: Using binary o 3 min read Relational Operators in C In C, relational operators are the symbols that are used for comparison between two values to understand the type of relationship a pair of numbers shares. The result that we get after the relational operation is a boolean value, that tells whether the comparison is true or false. Relational operato 4 min read std::string::append vs std::string::push_back() vs Operator += in C++ To append characters, you can use operator +=, append(), and push_back(). All of them helps to append character but with a little difference in implementation and application. Operator += : appends single-argument values. Time complexity : O(n)append() : lets you specify the appended value by using 6 min read Like