Board index » cppbuilder » Sort & Search of std::vectors

Sort & Search of std::vectors


2003-09-01 08:45:11 PM
cppbuilder111
Hi,
Havn't found specific functions to sort and search std::vectors. Should
therefore qsort, bsearch/lsearch and lfind still be considered The valid
options?
Rgds, Martin
 
 

Re:Sort & Search of std::vectors

"Martin Moeller" < XXXX@XXXXX.COM >wrote:
Quote
You know how you use the class-type of sort?

class MyClass;

typedef std::vector<MyClasst>MyClassVector;
typedef MyClassVector::iterator MyClassIterator

MyClassVector MyVector;

sort(MyVector.begin(),MyVector.end(), ???Compare Class??? );
An easy way to proceed is using the two-argument version of the algorithm.
In that case MyClass's operator < will be used.
class MyClass
{
...
bool operator < (const MyClass& other) const;
}
Daniel
 

Re:Sort & Search of std::vectors

Quote
An easy way to proceed is using the two-argument version of the algorithm.
In that case MyClass's operator < will be used.

class MyClass
{
...
bool operator < (const MyClass& other) const;
}

Thanks Daniel,
It's the whole afternoon i'm studying help & examples, but I don't think it
suits either: you 'define' your <operator for one (and only one) member of
MyClass.
And what do you do to compare/sort on other memebers of the same class? To a
ceratin extent you might be lucky if the cast is different, so that you can
overload the < operator, but what for a typical { AnsiString Name;
AnsiString Surname; } type?
I will start a new thread with a more practical request to solve.
Rgds, Martin
 

{smallsort}

Re:Sort & Search of std::vectors

"Martin Moeller" < XXXX@XXXXX.COM >writes:
Quote
It's the whole afternoon i'm studying help & examples, but I don't think it
suits either: you 'define' your <operator for one (and only one) member of
MyClass.
[Please be as careful as possible with terminology. When you write "member",
you seem to thinking of "instance".]
The operator is applied to one instance and passed another instance. These
two instances are compared by the operator.
 

Re:Sort & Search of std::vectors

"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote
"Martin Moeller" < XXXX@XXXXX.COM >writes:

>It's the whole afternoon i'm studying help & examples, but I don't think
it
>suits either: you 'define' your <operator for one (and only one) member
of
>MyClass.

[Please be as careful as possible with terminology. When you write
"member",
you seem to thinking of "instance".]

The operator is applied to one instance and passed another instance. These
two instances are compared by the operator.
 

Re:Sort & Search of std::vectors

"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >writes:
Quote
[Please be as careful as possible with terminology. When you write
"member",
you seem to thinking of "instance".]

Unfortunatelly I refer to 2 members (which is what he can't do!). Pls refer
to new thread with more complete spec of what I really mean/want.
Tks, martin
 

Re:Sort & Search of std::vectors

"Daniel Daranas" < XXXX@XXXXX.COM >writes:
Quote
"Martin Moeller" < XXXX@XXXXX.COM >wrote:

>You know how you use the class-type of sort?
>
>class MyClass;
>
>typedef std::vector<MyClasst>MyClassVector;
>typedef MyClassVector::iterator MyClassIterator
>
>MyClassVector MyVector;
>
>sort(MyVector.begin(),MyVector.end(), ???Compare Class??? );

An easy way to proceed is using the two-argument version of the algorithm.
In that case MyClass's operator < will be used.

class MyClass
{
...
bool operator < (const MyClass& other) const;
}

Daniel
Better, use a function object separate from the object you're sorting.
That way you are not bound to just one way to sort objects. For
example:
struct Person
{
int ssn;
std::string first_name;
std::string last_name;
int zip;
};
You may want to sort a group of Person records by name, ssn, zip, or
any other field. Building the operator< into the class itself is
restrictive.
I prefer to do this:
struct Person
{
int ssn;
std::string first_name;
std::string last_name;
int zip;
struct Sort_By_Last_Name;
struct Sort_By_SSN;
};
struct Person::Sort_By_Last_Name {
bool operator()(Person const & lhs, Person const & rhs) const
{ return lhs.last_name < rhs.last_name; }
};
struct Person::Sort_By_SSN {
bool operator()(Person const & lhs, Person const & rhs) const
{ return lhs.ssn < rhs.ssn; }
};
Then to use:
std::vector<Person>v;
initialize(v); // whatever.
std::sort(v.begin(), v.end(), Person::Sort_By_SSN());
Etc.
--
Chris(TeamB);