3-way comparison operator (Space Ship Operator) in C++ 20 Last Updated : 15 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the entire relationship in one query. Traditionally, strcmp() is such a function. Given two strings it will return an integer where, < 0 means the first string is less== 0 if both are equal> 0 if the first string is greater.It can give one of the three results, hence it’s a three-way comparison. EqualityOrderingPrimary==<=>Secondary!=<, >, <=, >=From the above table, it can be seen that the spaceship operator is a primary operator i.e., it can be reversed and corresponding secondary operators can be written in terms of it. (A <=> B) < 0 is true if A < B(A <=> B) > 0 is true if A > B(A <=> B) == 0 is true if A and B are equal/equivalent. Program 1: Below is the implementation of the three-way comparison operator for two float variables: C++ // C++ program to illustrate the 3 way comparison // (spaceship) operator #include <compare> #include <iostream> using namespace std; int main() { int x = 10; int y = 20; // saving the result of 3 way comparison operator auto res = x <=> y; // executing statements based on the above comparison if (res < 0) cout << "Less"; else if (res > 0) cout << "Greater"; else if (res == 0) cout << "Same"; else cout << "Unordered"; return 0; } Output Program 2: Below is the implementation of the three-way comparison operator for two vectors: C++ // C++ 20 program for the illustration of the // 3-way comparison operator for 2 vectors #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Given vectors vector<int> v1{ 3, 6, 9 }; vector<int> v2{ 3, 6, 9 }; auto ans2 = v1 <=> v2; // If ans is less than zero if (ans2 < 0) { cout << "v1 < v2" << endl; } // If ans is equal to zero else if (ans2 == 0) { cout << "v1 == v2" << endl; } // If ans is greater than zero else if (ans2 > 0) { cout << "v1 > v2" << endl; } return 0; } Output Note: You should download the adequate latest compiler to run C++ 20. Needs of Spaceship Operators: It's the common generalization of all other comparison operators (for totally-ordered domains): >, >=, ==, <=, <. Using <=>, every operation can be implemented in a completely generic way in the case of user-defined data type like a structure where one has to define the other 6 comparison operators one by one instead.For strings, it's equivalent to the old strcmp() function of the C standard library. So it is useful for lexicographic order checks, such as data in vectors, or lists, or other ordered containers. Comment More infoAdvertise with us Next Article Relational Operators on STL Array in C++ P parthbanathia Follow Improve Article Tags : C++ Programs Programming Language C++ cpp-operator Practice Tags : CPPcpp-operator Similar Reads How to Take Operator as Input in C++? Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as 2 min read How to Use the Not-Equal (!=) Operator in C++? The not-equal operator is a fundamental comparison operator in C++ represented by "!=". It is used for making decisions in programming and is hence called a conditional operator. In this article, we will discuss how to use the Not-Equal (!=) operator in C++. Not-Equal (!=) Operator in C++The not-equ 3 min read How to Sort a Vector Using a Custom Comparator in C++? In C++, the std::sort() function sorts the given vector in increasing order by default. The custom comparator is a function that defines the order in which the elements of a std::vector should be sorted. It is passed as the parameter to the std::sort() function.In this article, we will learn how to 4 min read Relational Operators on STL Array in C++ The article illustrates the working of the different relational operator on the array STL. The equality comparison ( == ) is performed by comparing the elements sequentially using operator ( == ), stopping at the first mismatch. The less-than comparison ( < ) or greater-than comparison ( > ) b 3 min read Relational Operators on STL Array in C++ The article illustrates the working of the different relational operator on the array STL. The equality comparison ( == ) is performed by comparing the elements sequentially using operator ( == ), stopping at the first mismatch. The less-than comparison ( < ) or greater-than comparison ( > ) b 3 min read How to Overload the Less-Than (<) Operator in C++? In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op 2 min read Like