Sign in to follow this  
lougv22

How to sort std::vector of custom objects?

Recommended Posts

So I am storing a number of objects of the custom type StaticImage in a std::vector. One of the members of the class is d_x which is the X coordinate. Something like this: class StaticImage { ... ... float d_x; ... ... } So basically I need to be able to sort the vector by d_x. Here is how I attempt to do it: struct compareImageByXCoordinate : std::binary_function<CEGUI::StaticImage*, CEGUI::StaticImage*, bool> { bool operator()(CEGUI::StaticImage* imageOne, CEGUI::StaticImage* imageTwo) const { if (imageOne->getPosition().d_x < imageTwo->getPosition().d_x) return true; return false; } // End operator() }; std::sort(mZoneImages.begin(), mZoneImages.end(), compareImageByXCoordinate()); where mZoneImages is the std::vector storing all the StaticImage objects. But that doesn't seem to be working. What am I doing wrong here?

Share this post


Link to post
Share on other sites
Quote:
Original post by lougv22
[...]But that doesn't seem to be working.[...]
It seems you need to fix it.

In order for anybody to provide details on the solution, you need to provide details on the problem:
What happens that should not?
What doesn't happen that should?
Does any part or portion of it work as expected?
In what way does it not work?

Share this post


Link to post
Share on other sites
Quote:
Original post by CaspianB
First, you're passing in the name of your structure with parenthesis at the end of the name. Second, I believe you need to pass in an actual object, not a class/structure name.
[smile] Actually, your first point is not an error, it's the solution to your second point. What that code does is create a temporary instance of the structure by calling its constructor.

Share this post


Link to post
Share on other sites
Gah, you replied while I deleted. Yeah I quickly wrote a test program and realized what I said was incorrect. My mistake. Those random oddities of C++ that I never use... heh.

Share this post


Link to post
Share on other sites
Quote:
Gah, you replied while I deleted.
It is the sacred duty of my clan to seek out and correct mistakes before they are deleted [evil]

All hail the holy Oluseyi and his prophet Zahlman!

Share this post


Link to post
Share on other sites
std::sort works with operator< I believe I would define that in addition to ().

Also, by "not work" I assume you mean it remains in the same order? Please specify.

EDIT: to clarify: you can overload the < operator in your StaticImage class and then not even pass a third argument to std::sort.

Share this post


Link to post
Share on other sites
Quote:
Original post by vs322
std::sort works with operator< I believe I would define that in addition to ().

Also, by "not work" I assume you mean it remains in the same order? Please specify.

EDIT: to clarify: you can overload the < operator in your StaticImage class and then not even pass a third argument to std::sort.


That's not useful here because the vector contains pointers to his instances, not the instances themselves. std::sort compares what's in the container; it doesn't care if they're pointers - it will sort the pointer values, not the pointed-at things.

@OP: Nothing looks amiss there. In what sense does it "not seem to be working"? Can you show a complete, minimal example?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by vs322
std::sort works with operator< I believe I would define that in addition to ().

Also, by "not work" I assume you mean it remains in the same order? Please specify.

EDIT: to clarify: you can overload the < operator in your StaticImage class and then not even pass a third argument to std::sort.


That's not useful here because the vector contains pointers to his instances, not the instances themselves. std::sort compares what's in the container; it doesn't care if they're pointers - it will sort the pointer values, not the pointed-at things.

@OP: Nothing looks amiss there. In what sense does it "not seem to be working"? Can you show a complete, minimal example?


Um, He I don't see where he is using a vector of pointers:
Quote:
Original post by lougv22
where mZoneImages is the std::vector storing all the StaticImage objects.


His compareImageByXCoordinate function takes pointers, but the vector itself is just of the object. (unless my reading comp is dead today.)

Share this post


Link to post
Share on other sites
Quote:
Original post by vs322
Quote:
Original post by Zahlman
Quote:
Original post by vs322
std::sort works with operator< I believe I would define that in addition to ().

Also, by "not work" I assume you mean it remains in the same order? Please specify.

EDIT: to clarify: you can overload the < operator in your StaticImage class and then not even pass a third argument to std::sort.


That's not useful here because the vector contains pointers to his instances, not the instances themselves. std::sort compares what's in the container; it doesn't care if they're pointers - it will sort the pointer values, not the pointed-at things.

@OP: Nothing looks amiss there. In what sense does it "not seem to be working"? Can you show a complete, minimal example?


Um, He I don't see where he is using a vector of pointers:
Quote:
Original post by lougv22
where mZoneImages is the std::vector storing all the StaticImage objects.


His compareImageByXCoordinate function takes pointers, but the vector itself is just of the object. (unless my reading comp is dead today.)


It's common for people to say (incorrectly) that they are storing objects in a vector when they actually are storing pointers thereto. I assumed the OP would not be spontaneously involving pointers in writing compareImageByXCoordinate for no reason. And anyway, that would cause the error to be at compile-time, in which case an OP who is resourceful enough to look up std::binary_function in the first place (a) ought to be able to decipher the compiler errors and (b) would give a better description than "it doesn't seem to work" (which usually indicates a run-time failure).

But it's possible I extend the OP too much credit. :)

Share this post


Link to post
Share on other sites
Extrarius is right, I should have provided more detail about what was not working. The problem was that the entries in the vector were not sorted after being passed to the function compareImageByXCoordinate.

After tinkering with it for a bit I figured out the reason why they weren't getting sorted was that all of them had a value of d_x = 0, which was due to something else in my code not related in any way to the function in question.

So after I passed (to the function) the vector with the actual d_x values it worked just fine. So there wasn't anything wrong with it after all.

But thanks to all who responded anyway.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this