473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to make the operator < ( ) work ?

ddh
Hi:

I have a class:
class CA
{
private:
int m_val;
};

and another
class CB
{
};
and then I use a std::map to contain the CA & CB pairs:

typedef std::map<CA *, CB *> CMyMap;

Then I get some CA *, CB * to insert into the map:

CA *pA1 = new CA()
CB *pB1 = new CB();
map.insert (std::make_pair(pA1, pB1));

CA *pA2 = new CA();
CB *pB2 = new CB();
map.insert (std::make_pair(pA2, pB2));

But the problem arise, when the second time i insert a pair, it use the
value of the point for comparing. That is to say, if pA2 < pA1, pA2
will be inserted in front of pA1 and vice verse. Which is not what I
want.

So I modify the class CA as:
class CA
{
public:
bool operator < (CA *pA)
{
return m_val < pA->m_val;
}
private:
m_val;
}

But it doesn't work. How can I get to use a user defined compare
function instead the comparion of the raw pointer?

Thank you for your help very much!!

Jul 23 '05 #1
3 1175
ddh wrote:
I use a std::map to contain the CA & CB pairs:

typedef std::map<CA *, CB *> CMyMap;

Then I get some CA *, CB * to insert into the map:

CA *pA1 = new CA()
CB *pB1 = new CB();
STL uses Value Semantics only (Don't ask why, C++ is not a reasonable
computer language). What you do probably compiles but is against the
design principles of STL. If not semantically wrong do

typedef std::map<CA, CB *> CMyMap;

or even better:

typedef std::map<CA, CB> CMyMap;
But the problem arise, when the second time i insert a pair, it use the
value of the point for comparing. That is to say, if pA2 < pA1, pA2
will be inserted in front of pA1 and vice verse. Which is not what I
want.

So I modify the class CA as:
class CA
{
public:
bool operator < (CA *pA)
friend bool operator < (const CA& left, const CA& right) {
return left.m_val < right.m_val;
}

{
return m_val < pA->m_val;
}
private:
m_val;
}

But it doesn't work. How can I get to use a user defined compare
function instead the comparion of the raw pointer?


If you really want it (although it's a bad idea):
struct deref_compare {
bool operator< (const CA* left, const CA* right) const {
return *left < *right;
}
}

std::map<CA*, CB*, deref_compare> mymap;

Jul 23 '05 #2
ddh wrote:
Hi:

I have a class:
class CA
{
private:
int m_val;
};

and another
class CB
{
};
and then I use a std::map to contain the CA & CB pairs:

typedef std::map<CA *, CB *> CMyMap;
Why pointers?
Then I get some CA *, CB * to insert into the map:

CA *pA1 = new CA()
CB *pB1 = new CB();
map.insert (std::make_pair(pA1, pB1));

CA *pA2 = new CA();
CB *pB2 = new CB();
map.insert (std::make_pair(pA2, pB2));

But the problem arise, when the second time i insert a pair, it use the
value of the point for comparing. That is to say, if pA2 < pA1, pA2
will be inserted in front of pA1 and vice verse. Which is not what I
want.

So I modify the class CA as:
class CA
{
public:
bool operator < (CA *pA)
{
return m_val < pA->m_val;
}
private:
m_val;
}

But it doesn't work.
Of course not. That operator compares a CA with a pointer to CA, not two
pointers. And before you try: No, you cannot make one that compares two
pointers.
How can I get to use a user defined compare function instead the comparion
of the raw pointer?


Use the map's 3rd template argument.

Jul 23 '05 #3

"Rapscallion" <ra********@spambob.com> skrev i en meddelelse
news:11**********************@g49g2000cwa.googlegr oups.com...
ddh wrote:
I use a std::map to contain the CA & CB pairs:

typedef std::map<CA *, CB *> CMyMap;

Then I get some CA *, CB * to insert into the map:

CA *pA1 = new CA()
CB *pB1 = new CB();
STL uses Value Semantics only (Don't ask why, C++ is not a reasonable
computer language).


Why? Do you find it more reasonable to use values semantics sometimes and
pointer semantics at other times? With the developer having no choice but to
use the choices made by the language implementor.
What you do probably compiles but is against the
design principles of STL. If not semantically wrong do

typedef std::map<CA, CB *> CMyMap;

or even better:

typedef std::map<CA, CB> CMyMap;
That entirely depends on the purpose of the OP. Both are correct, of course.
But the problem arise, when the second time i insert a pair, it use the
value of the point for comparing. That is to say, if pA2 < pA1, pA2
will be inserted in front of pA1 and vice verse. Which is not what I
want.

So I modify the class CA as:
class CA
{
public:
bool operator < (CA *pA)
friend bool operator < (const CA& left, const CA& right) {
return left.m_val < right.m_val;
}

{
return m_val < pA->m_val;
}
private:
m_val;
}

But it doesn't work. How can I get to use a user defined compare
function instead the comparion of the raw pointer?


If you really want it (although it's a bad idea):


Why is it a bad idea? If the user believes that pointers were the correct
solution but wants his sort to be made according to value-semantics, this is
a perfectly reasonable way to go.

struct deref_compare {
bool operator< (const CA* left, const CA* right) const {
return *left < *right;
}
}

std::map<CA*, CB*, deref_compare> mymap;

/Peter
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Victor | last post by:
Anyone knows how to write a virtual function for operator<< ? I have a base class and some public derived class from it. For the derived class, I hope they can use << to output their different...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
3
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends;...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
11
by: fungus | last post by:
I have some classes which have a "writeTo()" function but no operator<<. I want to fix it so that they all have an operator<< (for consistency). Can I do something like this: template <class...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
3
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.