C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sorting a vector of pointers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ari
Guest





PostPosted: Sat Jul 30, 2005 10:13 am    Post subject: sorting a vector of pointers Reply with quote



I have a vector of pointers to an abstract data type that I would like
to sort by a field in the data type. In abbreviated form:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}
};

std::vector<tDebugField*> sDebugPanelDisplays;

sDebugPanelDisplays.push_back(new tDebugField( "c" );
sDebugPanelDisplays.push_back(new tDebugField( "b" );
sDebugPanelDisplays.push_back(new tDebugField( "a" );
sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end());

And I want the order to be: a, b, c.

I've tried to write the following in same file:

bool operator<(const tDebugField* &lhs, const tDebugField* &rhs)
{
return lhs->name < rhs->name;
}

But I get the syntax error:
error C2803: 'operator <' must have at least one formal parameter of
class type

I've also tried to write:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}

bool operator<( const tDebugField &rhs)
{
return name < rhs.name;
}
};

But this doesn't give me the result that I want, either.

Thanks.

Ari


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bo Persson
Guest





PostPosted: Sat Jul 30, 2005 11:51 am    Post subject: Re: sorting a vector of pointers Reply with quote




"Ari" <alamstein (AT) maxis (DOT) com> skrev i meddelandet
news:1122690702.221341.181800 (AT) g47g2000cwa (DOT) googlegroups.com...
Quote:
I have a vector of pointers to an abstract data type that I would like
to sort by a field in the data type. In abbreviated form:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}
};

std::vector<tDebugField*> sDebugPanelDisplays;

sDebugPanelDisplays.push_back(new tDebugField( "c" );
sDebugPanelDisplays.push_back(new tDebugField( "b" );
sDebugPanelDisplays.push_back(new tDebugField( "a" );
sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end());

And I want the order to be: a, b, c.

I've tried to write the following in same file:

bool operator<(const tDebugField* &lhs, const tDebugField* &rhs)
{
return lhs->name < rhs->name;
}

But I get the syntax error:
error C2803: 'operator <' must have at least one formal parameter of
class type

You cannot define a comparison for pointers (or any fundamenal types)
because it is already available in the core language.

You can call it something else though, and pass it explicitly to the
sort routine

bool CompareDebugFields(etc
{ return lhs->name < rhs->name; }

std::sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end(),
CompareDebugFields);



Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bo Persson
Guest





PostPosted: Sat Jul 30, 2005 11:51 am    Post subject: Re: sorting a vector of pointers Reply with quote




"Ari" <alamstein (AT) maxis (DOT) com> skrev i meddelandet
news:1122690702.221341.181800 (AT) g47g2000cwa (DOT) googlegroups.com...
Quote:
I have a vector of pointers to an abstract data type that I would like
to sort by a field in the data type. In abbreviated form:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}
};

std::vector<tDebugField*> sDebugPanelDisplays;

sDebugPanelDisplays.push_back(new tDebugField( "c" );
sDebugPanelDisplays.push_back(new tDebugField( "b" );
sDebugPanelDisplays.push_back(new tDebugField( "a" );
sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end());

And I want the order to be: a, b, c.

I've tried to write the following in same file:

bool operator<(const tDebugField* &lhs, const tDebugField* &rhs)
{
return lhs->name < rhs->name;
}

But I get the syntax error:
error C2803: 'operator <' must have at least one formal parameter of
class type

You cannot define a comparison for pointers (or any fundamenal types)
because it is already available in the core language.

You can call it something else though, and pass it explicitly to the
sort routine

bool CompareDebugFields(etc
{ return lhs->name < rhs->name; }

std::sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end(),
CompareDebugFields);



Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carlos Moreno
Guest





PostPosted: Sat Jul 30, 2005 4:50 pm    Post subject: Re: sorting a vector of pointers Reply with quote

Ari wrote:
Quote:
I have a vector of pointers to an abstract data type that I would like
to sort by a field in the data type. In abbreviated form:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}
};

std::vector<tDebugField*> sDebugPanelDisplays;

sDebugPanelDisplays.push_back(new tDebugField( "c" );
sDebugPanelDisplays.push_back(new tDebugField( "b" );
sDebugPanelDisplays.push_back(new tDebugField( "a" );
sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end());

And I want the order to be: a, b, c.

I've tried to write the following in same file:

bool operator<(const tDebugField* &lhs, const tDebugField* &rhs)
{
return lhs->name < rhs->name;
}

But I get the syntax error:
error C2803: 'operator <' must have at least one formal parameter of
class type

What's happening here is that you're attempting to define an operator
that acts on built-in types (pointers, in this case). You're not
allowed to do that. The compiler knows how to compare two pointers.

Quote:
I've also tried to write:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}

bool operator<( const tDebugField &rhs)
{
return name < rhs.name;
}
};

Which is not what you want. You're telling the compiler how to compare
two tDebugField objects. What you have to "teach" the compiler how to
do is compare two tDebugField objects *given two pointers* to
tDebugField objects.

You need a function object here:

class cmp_DebugField_ptr
{
public:
bool operator() (const tDebugField * f1,
const tDebugField * f2) const
{
return f1->name < f2->name;
}
};

Because std:sort will pass pairs of elements of the sequence (and
your elements are pointers), and expect the predicate to return
whether the left-hand is less than the right-hand -- that's what
the above function object does.

You now call sort like:

sort (fields.begin(), fields.end(), cmp_DebugField_ptr());


HTH,

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dietmar Kuehl
Guest





PostPosted: Sun Jul 31, 2005 11:37 am    Post subject: Re: sorting a vector of pointers Reply with quote

Ari wrote:
Quote:
I've tried to write the following in same file:

bool operator<(const tDebugField* &lhs, const tDebugField* &rhs)
{
return lhs->name < rhs->name;
}

You cannot overload operators on built-in types and pointers are
considered built-in. This in turn mean that you cannot use the
default comparator with 'std::sort()'. You can simply write a
compare function or object and pass it to 'std::sort()' as third
parameter, e.g.:

struct name_compare
{
bool operator()(tDebugField const* lhs, tDebugField const* rhs) const
{
return lhs->name < rhs->name;
}
};
std::sort(sDebugPanelDisplays.begin() sDebugPanelDisplays.end(),
name_compare());
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
axter
Guest





PostPosted: Sat Aug 06, 2005 11:24 am    Post subject: Re: sorting a vector of pointers Reply with quote


Ari wrote:
Quote:
I have a vector of pointers to an abstract data type that I would like
to sort by a field in the data type. In abbreviated form:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}
};

std::vector<tDebugField*> sDebugPanelDisplays;

sDebugPanelDisplays.push_back(new tDebugField( "c" );
sDebugPanelDisplays.push_back(new tDebugField( "b" );
sDebugPanelDisplays.push_back(new tDebugField( "a" );
sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end());

And I want the order to be: a, b, c.

I've tried to write the following in same file:

bool operator<(const tDebugField* &lhs, const tDebugField* &rhs)
{
return lhs->name < rhs->name;
}

But I get the syntax error:
error C2803: 'operator <' must have at least one formal parameter of
class type

I've also tried to write:

struct tDebugField
{
std::string name;

tDebugField(const char *n) : name(n) {}

bool operator<( const tDebugField &rhs)
{
return name < rhs.name;
}
};

But this doesn't give me the result that I want, either.

Thanks.


You can try using a smart pointer like in the following link:
http://code.axter.com/clone_ptr.h

The above smart pointer has an operator<() function that calls the
object's operator<().
Example code:
#include "clone_ptr.h"

void TestSortVectorOfSmartPointers()
{
std::vector sDebugPanelDisplays;
sDebugPanelDisplays.push_back(new tDebugField( "c" ));
sDebugPanelDisplays.push_back(new tDebugField( "b" ));
sDebugPanelDisplays.push_back(new tDebugField( "a" ));
sort(sDebugPanelDisplays.begin(), sDebugPanelDisplays.end());
for (int i = 0;i < sDebugPanelDisplays.size();++i)
{
cout << sDebugPanelDisplays[i]->name << endl;
}
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.