use object itself as key in stl map

Y

yccheok

I have an object which will be used as a key in stl map. Although I had
overridden all the comparison operator (==, >, <, >=, <=), the vc++
compiler still makes complain on:

Compiling...
point.cpp
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) :
error C2678: binary '<' : no operator defined which takes a left-hand
operand of type 'const class Point' (or there is no acceptable
conversion)
C:\Program Files\Microsoft Visual
Studio\VC98\INCLUDE\functional(86) : while compiling class-template
member function 'bool __thiscall std::less<class Point>::eek:perator
()(const class Point &,const class Point &) const'
Error executing cl.exe.

point.exe - 1 error(s), 0 warning(s)

Any suggestion/ advice is very much appreciated.

cheok


#include <map>
#include <assert.h>

using namespace std;

class Point {
public:
Point() {
this->x = 0;
this->y = 0;
}

Point(int x, int y) {
this->x = x;
this->y = y;
}

Point(const Point &p) {
this->x = p.x;
this->y = p.y;
}

Point& operator=(const Point &p) {
this->x = p.x;
this->y = p.y;

return *this;
}

bool operator== (const Point &p) {
return (this->x == p.x && this->y == p.y);
}

bool operator> (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y > p.y);
}

bool operator< (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y < p.y);
}

bool operator<= (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y <= p.y);
}

bool operator>= (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y >= p.y);
}

private:
int x;
int y;
};

int main() {
map<Point, Point> m;

Point p1(100, 100);
Point p2(100, 100);

if(m.find(p1) == m.end()) {
m[p1] = p1;
}

// Although p1 and p2 are different objects,
// their content is the same through comparison on their
// private member variables x and y.
//
// Hence, p2 shouldn't be inserted into the map.
//
if(m.find(p2) == m.end()) {
m[p2] = p2;
assert(0);
}

return 1;
}
 
N

Neelesh

I have an object which will be used as a key in stl map. Although I had
overridden all the comparison operator (==, >, <, >=, <=), the vc++
compiler still makes complain on:
#include <map>
#include <assert.h>

using namespace std;

class Point {
public:

bool operator< (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y < p.y);

make operator< a const function
bool operator<(const Point & p) const {
// the code
}
}

int main() {
map<Point, Point> m;

Point p1(100, 100);
Point p2(100, 100);

if(m.find(p1) == m.end()) {
m[p1] = p1;

The stl container map is an ordered collection of pairs, and the
default ordering is done using the function object less(). This
function object defines operator() by taking two const references and
compares them using corresponding operator<. Hence, when you invoke
"find" on m, the less() function eventually calls Point::eek:perator<()
and the arguments that it passes are const references. Hence
Point::eek:perator<() must be declared const.

Hope this helps.
 
M

Mike Wahler

I have an object which will be used as a key in stl map. Although I had
overridden all the comparison operator (==, >, <, >=, <=), the vc++
compiler still makes complain on:

Compiling...
point.cpp
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) :
error C2678: binary '<' : no operator defined which takes a left-hand
operand of type 'const class Point' (or there is no acceptable
conversion)
C:\Program Files\Microsoft Visual
Studio\VC98\INCLUDE\functional(86) : while compiling class-template
member function 'bool __thiscall std::less<class Point>::eek:perator
()(const class Point &,const class Point &) const'
Error executing cl.exe.

point.exe - 1 error(s), 0 warning(s)

Any suggestion/ advice is very much appreciated.

cheok


#include <map>
#include <assert.h>

using namespace std;

class Point {
public:
bool operator< (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y < p.y);
}

friend bool operator<(const Point& lhs, const Point& rhs)
{
return lhs.y < rhs.y;
}

-Mike
 
M

Mark P

I have an object which will be used as a key in stl map. Although I had
overridden all the comparison operator (==, >, <, >=, <=), the vc++
compiler still makes complain on:

On a separate note from the suggestions given by the previous replies:

If you're going to use the object as its own key then you should use
std::set rather than std::map. (A set is essentially a map where the
objects are their own keys.)

Mark
 
N

Neil Cerutti

I have an object which will be used as a key in stl map.
Although I had overridden all the comparison operator (==, >,
<, >=, <=), the vc++ compiler still makes complain on:

My first suggestion is to use a std::set instead of a map. Why
double your storage requirements?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top