How to sort std::vector of custom objects?

Started by
9 comments, last by lougv22 15 years, 8 months ago
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?
Advertisement
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?
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
"But it does not seem to be working" conveys exactly zero useful information that could help us solve the problem, so I'll just assume that you forgot to turn your computer on. Turn it on, and try again.

As a side note, why not use a plain old function, instead of a functor, here?
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.
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.
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!
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.
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?
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.)
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. :)

This topic is closed to new replies.

Advertisement