Click to See Complete Forum and Search --> : STL Vector Array
Angie
June 8th, 1999, 08:02 AM
I created a vector array:
typedef std::vector<CAngleReading*> ANTENNA_ANGLE_VECTOR;
ANTENNA_ANGLE_VECTOR m_VertList;
I filled the array and now that I have it filled I want to sort it. I have read a little about sorting and I am not sure about how to do the sorting. If anyone has any ideas I would appreciate it.
Thanks,
Angie.
Josh Handley
June 8th, 1999, 08:47 AM
There is a sort function that you can use called sort. It has two forms:
void sort(RandomAccessIterator first, RandomAccessIterator last);
that works if the type you are sorting has an operator<.
For example:
vector<int> v;
sort(v.begin(), v.end());
The other form is:
void sort(RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp);
where you supply the comparison functor.
For example:
struct IsLessThan {
bool operator() (MyClass a, MyClass b) const
{
return whether or not a is less than b
}
};
vector<MyClass> v;
IsLessThan compFunc;
sort(v.begin(), v.end(), compFunc);
Josh
Dave Lorde
June 8th, 1999, 09:02 AM
Josh is right, but be aware that STL containers were designed to be value-based, so the default operations for sorting, etc., rely on objects being stored, rather than pointers. If you store pointers, you'll always have to use the predicate form of these algorithms, which makes a bit more work writing the functors.
Dave
p.s. convention has it that all capital names are only used for preprocessor constants (#define's).
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.