I am trying to create a set of struct in C++, an action requiring to overload the '<' operator.
Even though I did find a way to make it compile, it does not get encapsulated within the struct, therefore making it look unclean for OO design.
What I have tried which works:
#include <iostream>
#include <set>
struct Coordinate {
int x, y;
};
bool operator<(const Coordinate& l, const Coordinate& r) {
return l.x * l.y < r.x * r.y;
}
int main() {
std::set<Coordinate> mySet;
mySet.insert(Coordinate{ 5,5 });
}
What I would like to do is something like this:
#include <iostream>
#include <set>
struct Coordinate {
int x, y;
bool operator<(const Coordinate& other) {
return this->x * this->y < other.x * other.y;
}
};
int main() {
std::set<Coordinate> mySet;
mySet.insert(Coordinate{ 5,5 });
}
However the latter does not compile.
error message:
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xstddef(127,1): error C2678: binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=Coordinate
1> ]
1>C:\CppWorkspace\Dungeons of Doom\Dungeons of Doom\src\TestMain.cpp(7,7): message : could be 'bool Coordinate::operator <(const Coordinate &)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xstddef(127,1): message : while trying to match the argument list '(const _Ty, const _Ty)'
1> with
1> [
1> _Ty=Coordinate
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xstddef(126): message : while compiling class template member function 'bool std::less<Coordinate>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=Coordinate
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xutility(1469): message : see reference to function template instantiation 'bool std::less<Coordinate>::operator ()(const _Ty &,const _Ty &) const' being compiled
1> with
1> [
1> _Ty=Coordinate
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xmemory(1318): message : see reference to class template instantiation 'std::less<Coordinate>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\xmemory(1318): message : see reference to variable template 'const bool is_empty_v<std::less<Coordinate> >' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.25.28508\include\set(54): message : see reference to class template instantiation 'std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>' being compiled
1> with
1> [
1> _Kty=Coordinate,
1> _Pr=std::less<Coordinate>,
1> _Alloc=std::allocator<Coordinate>
1> ]
1>C:\CppWorkspace\Dungeons of Doom\Dungeons of Doom\src\TestMain.cpp(17): message : see reference to class template instantiation 'std::set<Coordinate,std::less<Coordinate>,std::allocator<Coordinate>>' being compiled
I am new to C++ and was wondering whether there is a way to do something like this.
Did you look at the compile error?
As it says, the method needs to be const
bool operator<(const Coordinate& other) const { // const the < operator
return this->x * this->y < other.x * other.y;
}
It's better that the comparison operators be friend functions.
#include <iostream>
#include <set>
struct Coordinate {
int x, y;
friend bool operator<(const Coordinate& lhs, const Coordinate& rhs) {
return lhs.x * lhs.y < rhs.x * rhs.y;
}
};
int main() {
std::set<Coordinate> mySet;
mySet.insert(Coordinate{ 5,5 });
}
operator<
needs to be const
, but a bigger problem is that the operator doesn't do proper strict weak ordering that's mentioned in the Compare named requirement.
Consider this code:
#include <iostream>
#include <set>
struct Coordinate {
int x, y;
Coordinate(int X, int Y) : x(X), y(Y) {}
bool operator<(const Coordinate& other) const {
return this->x * this->y < other.x * other.y;
}
};
int main() {
std::set<Coordinate> mySet;
mySet.emplace(1, 5);
mySet.emplace(5, 1);
std::cout << mySet.size() << '\n';
}
This will output 1
since the second Coordinate
will be considered equal to the first when using that operator<
.
A proper version could look like this:
bool operator<(const Coordinate& other) const {
return x==other.x ? y < other.y : x < other.x;
}
Or better, use std::tie
from <tuple>
which simplifies this considerably:
bool operator<(const Coordinate& other) const {
return std::tie(x, y) < std::tie(other.x, other.y);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.