STL list sort
I've got a problem sorting some data structures in a list. They look something like this:
class data
{
//blah
float distance;
};
list< data > datalist;
I want to sort the datalist by the _distance value_.
What I've found so far is stl function objects (can pass one to the sort() function), but I can't get them to work..
Thanks in advance!
[edited by - Lord Maz on January 11, 2003 6:34:05 PM]
-Lord Maz-
#include <algorithm>#include <functional>struct SortDataByDistance : public std::binary_function<const Data&, const Data&, bool>{ result_type operator()( first_argument_type a, second_argument_type b ) { return a.distance < b.distance; }};mylist.sort( SortDataByDistance() );
or something like that.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Or you could do it like this:
ack! I can't get it to look decent no matter what tags I use! Ah well, you get the idea...
ai-junkie.com
[edited by - fup on January 11, 2003 7:34:37 PM]
#include <algorithm>#include <iostream>#include <list>using namespace std;struct data{ string name; double val; data(double v):val(v),name("blah"){} friend bool operator<(const data& lhs, const data& rhs) { return (lhs.val < rhs.val); }};int main(){ list DataList; for (int i=0; i<10; ++i) { DataList.push_back(data(10-i)); } DataList.sort(); for (list::iterator it = DataList.begin(); it != DataList.end(); ++it) { cout << it->val << ", "; } return 0;}
ack! I can't get it to look decent no matter what tags I use! Ah well, you get the idea...
ai-junkie.com
[edited by - fup on January 11, 2003 7:34:37 PM]
My Website: ai-junkie.com | My Books: 'Programming Game AI by Example' & 'AI Techniques for Game Programming'
While both methods would technically work, most the time it doesn''t make sense to give a lot of objects comparison operators. For example, if you have two vectors, what the heck is
v1 < v2
supposed to mean? On the other handed, if you wanted to sort vectors by their distance from the origin, it makes more sense to make a seperate predicate that does the work, rather than make it a property of the object. In addition, you can only have one < operator. What happens when you want to sort by different conditions?
While both methods would technically work, most the time it doesn''t make sense to give a lot of objects comparison operators
I have to disagree with that statement. I use them every day in my work. It depends what you are writing code to do. You can''t make generalizations.
In addition, you can only have one < operator. What happens when you want to sort by different conditions?
The OP didn''t want to sort by different conditions. He explicitly stated his requirements and Fruny and I gave him perfectly acceptable solutions.
ai-junkie.com
I have to disagree with that statement. I use them every day in my work. It depends what you are writing code to do. You can''t make generalizations.
In addition, you can only have one < operator. What happens when you want to sort by different conditions?
The OP didn''t want to sort by different conditions. He explicitly stated his requirements and Fruny and I gave him perfectly acceptable solutions.
ai-junkie.com
My Website: ai-junkie.com | My Books: 'Programming Game AI by Example' & 'AI Techniques for Game Programming'
quote:
Original post by Zipster
While both methods would technically work, most the time it doesn''t make sense to give a lot of objects comparison operators. For example, if you have two vectors, what the heck isv1 < v2
supposed to mean? On the other handed, if you wanted to sort vectors by their distance from the origin, it makes more sense to make a seperate predicate that does the work, rather than make it a property of the object. In addition, you can only have one < operator. What happens when you want to sort by different conditions?
That''s what Fruny''s method does.
SortDataByDistance
is a functor which sorts a Data
object list by distance... If I had my way, I''d have all of you shot!
codeka.com - Just click it.
quote:
That's what Fruny's method does. SortDataByDistance is a functor which sorts a Data object list by distance...
Um, yes, it does...
quote:
I have to disagree with that statement. I use them every day in my work. It depends what you are writing code to do. You can't make generalizations.
I never said overloading the comparison operator for an object doesn't make sense, I said that it doesn't make sense for certain objects to have comparison operators, for clarity's sake if anything. It's like overloading the * operator on a vector to mean the dot product. It might make sense to you, but to other people it might not be clear. Using my previous example, would you add < and > operators to a vector?
quote:
The OP didn't want to sort by different conditions. He explicitly stated his requirements and Fruny and I gave him perfectly acceptable solutions.
The point of my post was that I prefer Fruny's solution over adding an operator to a class if the meaning of that operator isn't obvious. And while the original poster did give specific criteria for his class, I simply wanted to point out the difference between the two methods, as well as some limitations, because it was clear that the poster was a little confused about using STL's sort functionality. Don't take it personally.
[edited by - Zipster on January 11, 2003 12:33:08 AM]
quote:
Original post by Zipster
Um, yes, it does...
Oh, I assumed when you said "while both methods would technically work..." that you were referring to Fruny''s as well... never mind, then

If I had my way, I''d have all of you shot!
codeka.com - Just click it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement