Thread: [Dev-C++] STL sort() problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Abhijit S. <mu...@gm...> - 2002-08-18 08:48:08
|
Greets.
I'm using a vector of a structure pointer (I use dynamic allocation), like
so:
vector<PlsLine *> lines;
and PlsLine is a structure:
typedef struct _PlsLine
{
char *fn; // Full filename (c:\foo\bar.mp3)
char *filename; // Filename part only (bar.mp3)
int size; // Filesize
} PlsLine;
I need to sort this, and I call sort() like this:
sort(lines.begin(), lines.end());
However, sort() uses the comparison operators (< and >), and I can't define
them for pointers... The compiler flags this:
bool operator <(const PlsLine *&left, const PlsLine *&right)
as an error. It says it should be a class or enumerated type. I tried doing
this:
bool operator <(const PlsLine &left, const PlsLine &right)
and the compile worked, but it doesn't seem to sort.
Any work-around?
___________________________________________________________
Abhijit Shylanath
E-mail: mu...@gm... || ibr...@bi...
Web-site: http://mudeth.tripod.com/
|
|
From: Eddy L O J. <ed...@kl...> - 2002-08-18 10:10:42
|
2002-08-18 10:46:45, "Abhijit Shylanath" <mu...@gm...> wrote:
>I'm using a vector of a structure pointer (I use dynamic
>allocation), like
I'm in a good mood today, so I wrote you up a short example using
a functor predicate. I'm sure it can be solved using an overloaded
operator<() too, that's just now how I'd do it.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
class on_age
{
public:
bool operator() (const T& v1, const T& v2) const
{
return v2->age_ > v1->age_;
}
};
struct t_person
{
t_person(const string& name, int age) : name_(name), age_(age)
{ };
string name_;
int age_;
};
int main(int argc, char *argv[])
{
vector<t_person*> people;
vector<t_person*>::const_iterator it;
people.push_back( new t_person("Gordon", 31) );
people.push_back( new t_person("Tiny", 1) );
people.push_back( new t_person("Alice", 16) );
people.push_back( new t_person("Second", 2) );
people.push_back( new t_person("Grandpa", 101) );
sort(people.begin(), people.end(), on_age<t_person*>() );
for( it = people.begin() ; it != people.end() ; ++it )
cout << (*(it))->name_ << ", " << (*(it))->age_ << endl;
// yeah, yeah, "leak". Whatever
return 0;
}
--
"Reading this line brought you three seconds closer to death."
Eddy L O Jansson | http://gazonk.org/~eloj
|
|
From: Ioannis V. <no...@ho...> - 2002-08-18 21:11:43
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Abhijit Shylanath
> Sent: Sunday, August 18, 2002 11:47 AM
> To: Dev-C++ Mailing List
> Subject: [Dev-C++] STL sort() problem
>
>
> Greets.
>
> I'm using a vector of a structure pointer (I use dynamic
> allocation), like
> so:
>
> vector<PlsLine *> lines;
>
> and PlsLine is a structure:
>
> typedef struct _PlsLine
> {
> char *fn; // Full filename (c:\foo\bar.mp3)
> char *filename; // Filename part only (bar.mp3)
> int size; // Filesize
> } PlsLine;
>
> I need to sort this, and I call sort() like this:
>
> sort(lines.begin(), lines.end());
>
> However, sort() uses the comparison operators (< and >), and
> I can't define
> them for pointers... The compiler flags this:
>
> bool operator <(const PlsLine *&left, const PlsLine *&right)
>
> as an error. It says it should be a class or enumerated type.
Yes you can not have a reference to a pointer with exception to
templates.
> I tried doing
> this:
>
> bool operator <(const PlsLine &left, const PlsLine &right)
>
> and the compile worked, but it doesn't seem to sort.
> Any work-around?
Yes, you probably made some logical error in the definition of
operator<(). Send us its definition.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|
|
From: Abhijit S. <mu...@gm...> - 2002-08-19 20:38:28
|
> > I tried doing > > this: > > > > bool operator <(const PlsLine &left, const PlsLine &right) > > > > and the compile worked, but it doesn't seem to sort. > > Any work-around? > > > Yes, you probably made some logical error in the definition of > operator<(). Send us its definition. > The mistake was that the vector was still instantiated with PlsLine *, not with PlsLine. It's working now, thanks. Just for the record, the other form of sort() which takes a comparison function also works, as Eddy said. Of course it works... it's meant to :). ___________________________________________________________ Abhijit Shylanath E-mail: mu...@gm... || ibr...@bi... Web-site: http://mudeth.tripod.com/ |
|
From: Ioannis V. <no...@ho...> - 2002-08-19 23:18:38
|
> -----Original Message----- > From: Abhijit Shylanath [mailto:mu...@gm...] > Sent: Monday, August 19, 2002 11:24 PM > To: Ioannis Vranos; Dev-C++ Mailing List > Subject: Re: [Dev-C++] STL sort() problem > > > > > I tried doing > > > this: > > > > > > bool operator <(const PlsLine &left, const PlsLine &right) > > > > > > and the compile worked, but it doesn't seem to sort. > > > Any work-around? > > > > > > Yes, you probably made some logical error in the definition of > > operator<(). Send us its definition. > > > > The mistake was that the vector was still instantiated with > PlsLine *, not > with PlsLine. It's working now, thanks. Good. > Just for the record, > the other form > of sort() which takes a comparison function also works, as Eddy said. It is the same sort(). It uses < as default. If the default works do not use a comparison function or function object. The default is usually more optimised. Also try to make the operator<() inline (either a member function or a regular function). Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys |