Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sorting vectors


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 31 July 2012 - 05:26 AM

Hi guys,

I have a vector of classes. One of the variables in the class instance is nZorder (a number between 1 & 100).

I want to be able to do a, one off, sort the vectors.

I have found reference to the sort() function. But, am not sure how to use it.

I have this, but it wount compile due to the last parameter.

sort(VectorAsset.begin(),VectorAsset.end(),VectorAsset.nZorder);

Any tips on how to use this function would be awesome.

Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4433

Like
1Likes
Like

Posted 31 July 2012 - 05:42 AM

struct MySortFunctor
{
   bool operator() (const MyClass& op0, const MyClass& op1) const
   {
	   return op0.nZorder < op1.nZorder; // or whatever
   }
};

sort(VectorAsset.begin(),VectorAsset.end(), MySortFunctor());
should do the trick, minus possible typos. Could be much easier if your compiler supports lambdas.

#3 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 31 July 2012 - 05:52 AM

Thanks for the help. No idea what it does though :)

I get a couple of errors when compiling, any chance of a hand on that too?

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '&'
error C2065: 'op0' : undeclared identifier
error C2228: left of '.nZorder' must have class/struct/union
error C2065: 'op1' : undeclared identifier
error C2228: left of '.nZorder' must have class/struct/union

#4 BitMaster   Crossbones+   -  Reputation: 4433

Like
1Likes
Like

Posted 31 July 2012 - 05:58 AM

If you copied the above code verbatim you have to replace MyClass with whatever is inside the vectors. If you have already done that and the errors persists the struct must be declared where whatever MyClass actually is is completely known.

#5 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 31 July 2012 - 06:11 AM

Cool, thanks for the advice. Everything compiles great now!

Although it doesn't seem to have resorted anything.

Does sort() permenantly reshuffle things into the new order or is it just for that call (or something)?

#6 BitMaster   Crossbones+   -  Reputation: 4433

Like
1Likes
Like

Posted 31 July 2012 - 06:19 AM

Use your debugger to inspect the vector right after the sort call. Unless the nZorder are not in the state you expect them to be or you added a faulty sort functor, the vector should be sorted. Check that.
If the vector is sorted right after the call but whatever happens later does not see the correctly sorted vector, all kinds of things could be wrong. You could be, for example, sort a copy of the vector in a function call, discard the sorted version at the end of the function and use the unsorted original vector afterwards. But that is impossible to say without code.

#7 arkane7   Members   -  Reputation: 213

Like
0Likes
Like

Posted 31 July 2012 - 12:33 PM

reference: http://www.engr.sjsu...tt/SortFind.htm just go down until it talks about sort


this is how i learned to use sort. using a function that is basically a < operator

bool lessOp (YourClass a, YourClass b)
{
if (a.attribute < b.attribute) return true
else return false;
}

then with your vector of objects, just call the sort function from the algorithm library

sort (vec.begin(), vec.end(), lessOp); /////// you're passing a pointer to your lessOp function; using this pointer sort will call your function to compare the objects

or doing what the website i used above, just create a operator< function for you classes like this:

bool operator<(const Cperson& left, const Cperson& right)
{
return left.lastname < right.lastname;
}

now whenever you have 2 objects of that class and you do this var1<var2 it will called that function above.

just use your class instead of Cperson

Edited by arkane7, 31 July 2012 - 12:39 PM.

Always improve, never quit.

#8 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 31 July 2012 - 03:43 PM

Use your debugger to inspect the vector right after the sort call. Unless the nZorder are not in the state you expect them to be or you added a faulty sort functor, the vector should be sorted. Check that.
If the vector is sorted right after the call but whatever happens later does not see the correctly sorted vector, all kinds of things could be wrong. You could be, for example, sort a copy of the vector in a function call, discard the sorted version at the end of the function and use the unsorted original vector afterwards. But that is impossible to say without code.


Cool, thanks for the advice. I'll have a look at that.

#9 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 31 July 2012 - 06:23 PM

It seems to be more a case of nZorder not being in the state I expected. The values are, indeed, not what they shoud be.

I'll have a look and let you know how I go.

Thanks for persisting with me :)

#10 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 31 July 2012 - 06:28 PM

LOL - it helps when you dont have cAsset.nZorder=nZorder; commented out when creating the instance. :)

BitMaster - You have been an awesome help! All is working perfectly now!

I can't thank you enough! +1 to all of your posts!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS