Jump to content
  • Advertisement
Sign in to follow this  
rherm23

Sorting Vector containers in C++

This topic is 2268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a struct like so ..


[source lang="cpp"]

struct STUFF

{

int points;

string first_name;

string last_name;

}
[/source]

I have a vector <STUFF> stuffs and i wanted to sort the data inside from highest to lowest using the pts value. Is something like that possible?
I havent used c++ in a while and i started fleshing out this program and was stumped on this problem. Im going to be displaying the players according to the pts value in each struct so the person with the most points should be at index [0] and the last place person at the last index.

Im gonna keep at it but i think that any ideas i might be having might be too complicated and a more elegant solution may exist.

Thanks in advance for any help and replies.

Share this post


Link to post
Share on other sites
Advertisement
Thanks for the replies. Ill tst them when i get home from work. I appreciate it. +reps to you both.

Share this post


Link to post
Share on other sites



sort(stuffs.begin(), stuffs.end(),
[](const STUFF& lhs, const STUFF& rhs) -> bool
{ return lhs.points < rhs.points; });




I have never seen anything like that in C++. Could you provide a link that explains it?

BTW, IMO, in a For Beginner's forum, you should explain what you've done, or give a simpler solution, as DevLiquidKnight did.

Share this post


Link to post
Share on other sites

[quote name='Bregma' timestamp='1341883765' post='4957470']


sort(stuffs.begin(), stuffs.end(),
[](const STUFF& lhs, const STUFF& rhs) -> bool
{ return lhs.points < rhs.points; });




I have never seen anything like that in C++. Could you provide a link that explains it?

BTW, IMO, in a For Beginner's forum, you should explain what you've done, or give a simpler solution, as DevLiquidKnight did.
[/quote]

I guess, what you have never seen is the lambda function used as the comparison operator. What it basically does, is to create an anonymous (unnamed) function, that can be passed directly to function templates such as sort. http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B should explain this well. Edited by rnlf

Share this post


Link to post
Share on other sites

I have a struct like so ..

[source lang="cpp"]
struct STUFF
{
int points;
string first_name;
string last_name;
}
[/source]

I have a vector <STUFF> stuffs and i wanted to sort the data inside from highest to lowest using the pts value. Is something like that possible?
I havent used c++ in a while and i started fleshing out this program and was stumped on this problem. Im going to be displaying the players according to the pts value in each struct so the person with the most points should be at index [0] and the last place person at the last index.

Im gonna keep at it but i think that any ideas i might be having might be too complicated and a more elegant solution may exist.

Thanks in advance for any help and replies.


It looks like an easy solution is to change std::vector into std::set. It is another container type that will sort the entries automatically. That means some changes, which may or may not be fine with you:

  • You can't access elements using []-operator. Instead you need to iterate "for (auto it = stuffs.begin(); it != stuffs.end(); it++)", but that will produce the elements in the order you want.
  • You add new elements with "stuffs.insert()" instead of "stuffs.push_back()".
  • You need to define a comparison operator for STUFF.
    Edited by larspensjo

Share this post


Link to post
Share on other sites
blink.png This lot, the discussion about C++11 compliance and VS compliance has gave me quiet a startle... now I wonder what I can use or cant, if an piece of code fails due to none compliance or bad coding on my part.

Made me also wonder if VS is the compiler for me, I only use it because I am using the express ( free ) version and I figured it would be pretty much up to date compliance wise. Makes me wonder if I shouldnt look to using some other compiler, especially if I can lose the hassle of convincing users of my code to get the runtime dll for VS10.

... but I suppose at my current level ( beginner - intermediate sort of ) it wont really effect me , right ?

Share this post


Link to post
Share on other sites
It will. If you try to learn modern C++, you should not have to learn all the old techniques which were required back in the day, but have been replaced with safer, easier and faster solutions in C++11. You get the GNU compiler collection for free, too, you get IDEs for every popular operating system for free. Why not profit from their quicker adoption of C++11?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!