Reputation: 127
I'm making a program that generates a maze and then uses bredth first search to find a way in the maze. My function that checks if an element is present in a container-class now uses the vector like this (where coordinatePath is a typedef for vector) :
bool Labyrinth::inVisited(const Coordinate &c, const coordinatePath &visited ) const
{
for each (Coordinate coord in visited)
{
if(coord == c)
return true;
}
return false;
}
Since this method has to traverse the full container if an element is not present it's very ineffective for large searches. I tried to implement the same function that uses a set instead of a vector and wrote it like this:
bool Labyrinth::inVisited(const Coordinate &c, const set<Coordinate> &visited ) const
{
return (visited.find(c) != visited.end());
}
when i try to recomplie i get a lot of errors where the topmost is
Error 22 error C2676: binary '<' : 'const Coordinate' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef 193
I don't really understand these particular debug-messages and wonder if there is a way to implement this faster search!
Upvotes: 0
Views: 133
Reputation: 59811
To use elements in a set
the value_type
has to define operator<
or you need to provide a comparison functor to the container. Apparently, your Coordinate
type doesn't do that or the operator<
you provide takes incompatible arguments. It should look roughly like this:
struct Coordinate {
bool operator<(const Coordinate& other) const { return false; }
};
// or by providing a functor
struct CmpCoord {
bool operator()(const Coordinate& x, const Coordinate& y);
};
typedef std::set<Coordinate, CmpCoord> coord_set;
Upvotes: 1
Reputation: 258568
In order to create a std::set
of objects, those objects have to define operator <
.
So you need to add the following operator:
inline bool operator < (const Coordinate& first, const Coordinate& other);
Upvotes: 2