🔒 Locked
C++ vector sorting
Advertisement
Original Post
Hey All, Im trying to figure out how to sort a vector, list ect that contains an object. I want to sort by a variable inside the object. Ive searched for hours and cant seem to find an actual example. Google is full of simplistic sorting numbers tho! Lets say your vector is full of: class test { public: int a; int b; int c; } you want to sort by int b - or whatever.. How would you do it? Thanks
all stl containers have a sort function either built in or in the algorithms header. the sort function takes a comparison function as the second argument.
Yea, understand the theory of how it is supposed to work..
However, can not find an example of the syntax which is what I dont understand..
Heres an example of what is on various sites::
int myints[] = {32,71,12,45,26,80,53,33};
vector myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector::iterator it;
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
Wonderful.. Except no example of sorting objects.
However, can not find an example of the syntax which is what I dont understand..
Heres an example of what is on various sites::
int myints[] = {32,71,12,45,26,80,53,33};
vector
vector
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
Wonderful.. Except no example of sorting objects.
You just have to write a function that says whether the first is less than, equal to, or greater than the second one. What, specifically, is giving you trouble? Ultimately, you are just sorting ints, because test.b is an int.
Example of custom sorting functor:
struct test { int a; int b; int c;};std::ostream &operator <<( std::ostream &out, const test &t ){ out << "{ " << t.a << ' ' << t.b << ' ' << t.c << " }"; return out;}struct SortByA { bool operator()( const test &one, const test &two ) const { return one.a < two.a; }};struct SortByB { bool operator()( const test &one, const test &two ) const { return one.b < two.b; }};int main(){ std::vector<test> v; for( int i = 0 ; i < 5 ; ++i ) { test t = {rand()%10,rand()%10,rand()%10}; v.push_back(t); } std::sort(v.begin(),v.end(),SortByA()); std::copy(v.begin(),v.end(),std::ostream_iterator<test>(std::cout, " ")); std::cout << '\n'; std::sort(v.begin(),v.end(),SortByB()); std::copy(v.begin(),v.end(),std::ostream_iterator<test>(std::cout, " ")); std::cout << '\n';}Quote:
Original post by rolkA Quote:
Original post by stonemetal
all stl containers have a sort function either built in or in the algorithms header. the sort function takes a comparison function as the second argument.
Not really, you can provide a predicate but if you dont, then < is used. And containers do not have any embeded sort function, you have to use std::sort. (try to use v.sort() with v being a vector then come back...).
Here is An example that illustrates what i said earlier :
*** Source Snippet Removed ***
so you are saying list.sort doesn't exist, interesting.
the predicate you will want to write is
bool sortstuff(element1, element2)
{return true if 1 should come before 2}
then you call sort(v.begin,v.end,sortstuff)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement