Board index » cppbuilder » Sort & Search of std::vectors
Martin Moeller
![]() CBuilder Developer |
Martin Moeller
![]() CBuilder Developer |
Sort & Search of std::vectors2003-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 |
Daniel Daranas
![]() CBuilder Developer |
2003-09-01 11:25:04 PM
Re:Sort & Search of std::vectors
"Martin Moeller" < XXXX@XXXXX.COM >wrote:
QuoteYou know how you use the class-type of sort? class MyClass { ... bool operator < (const MyClass& other) const; } Daniel |
Martin Moeller
![]() CBuilder Developer |
2003-09-01 11:31:08 PM
Re:Sort & Search of std::vectorsQuoteAn easy way to proceed is using the two-argument version of the algorithm. 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} |
maeder
![]() CBuilder Developer |
2003-09-01 11:48:06 PM
Re:Sort & Search of std::vectors
"Martin Moeller" < XXXX@XXXXX.COM >writes:
QuoteIt's the whole afternoon i'm studying help & examples, but I don't think it The operator is applied to one instance and passed another instance. These two instances are compared by the operator. |
Martin Moeller
![]() CBuilder Developer |
2003-09-01 11:50:50 PM
Re:Sort & Search of std::vectors
"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >ha scritto nel messaggio
Quote"Martin Moeller" < XXXX@XXXXX.COM >writes: |
Martin Moeller
![]() CBuilder Developer |
2003-09-01 11:52:28 PM
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 Tks, martin |
Chris Uzdavinis
![]() CBuilder Developer |
2003-09-02 12:47:04 AM
Re:Sort & Search of std::vectors
"Daniel Daranas" < XXXX@XXXXX.COM >writes:
Quote"Martin Moeller" < XXXX@XXXXX.COM >wrote: 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); |