Compare two objects in C++
This post will discuss how to compare two objects in C++.
We can determine whether two objects are the same by implementing a comparison operator== for the class. For instance, the following code compares two objects of class Node based on the value of x and y field.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> class Node { public: int x, y; Node(int x, int y): x(x), y(y) {} bool operator==(const Node& rhs) const; }; bool Node::operator==(const Node& rhs) const { return x == rhs.x and y == rhs.y; } int main() { Node node1(1, 1); Node node2(1, 1); bool isEqual = (node1 == node2); std::cout << std::boolalpha << isEqual << std::endl; return 0; } |
The object comparison should be done on all the fields that are critical for determining its equality. Starting with C++11, we can construct a std::tie to easily match against multiple fields, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <tuple> class Node { public: int x, y; Node(int x, int y): x(x), y(y) {} bool operator==(const Node& rhs) const; }; bool Node::operator==(const Node& rhs) const { return std::tie(x, y) == std::tie(rhs.x, rhs.y); } int main() { Node node1(1, 1); Node node2(1, 1); bool isEqual = (node1 == node2); std::cout << std::boolalpha << isEqual << std::endl; return 0; } |
We can also implement the comparison operator!= to determine whether two objects of a class are not the same. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> class Node { public: int x, y; Node(int x, int y): x(x), y(y) {} bool operator==(Node const &rhs) const; bool operator!=(Node const &rhs) const; }; bool Node::operator==(Node const &rhs) const { return x == rhs.x and y == rhs.y; } bool Node::operator!=(Node const &rhs) const { return x != rhs.x or y != rhs.y; } int main() { Node node1(1, 1); Node node2(1, 1); bool isNotEqual = node1 != node2; std::cout << std::boolalpha << isNotEqual << std::endl; // false return 0; } |
Note that the equality operators operator== and operator!= need not be members of the class. It can be implemented as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> class Node { public: int x, y; Node(int x, int y): x(x), y(y) {} }; bool operator==(Node const &lhs, Node const &rhs) { return lhs.x == rhs.x and lhs.y == rhs.y; } bool operator!=(Node const &lhs, Node const &rhs) { return lhs.x != rhs.x or lhs.y != rhs.y; } int main() { Node node1(1, 1); Node node2(1, 1); bool isEqual = (node1 == node2); std::cout << std::boolalpha << isEqual << std::endl; // true return 0; } |
That’s all about comparing two objects in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)