Sorting Vector containers in C++

Started by
16 comments, last by Matt-D 11 years, 9 months ago
If you define some operators for it (< specifically), you should be able to use std::sort without having to do any lambda functions. Though it may be more fun/informative to do it that way (if you do it correctly).

edit: Ah. it looks like SoL mentioned this, but it was deep in his uber-post.
Advertisement
Well, I think this thread got side-tracked, and the OP is thoroughly confused, but I appreciate the explanation. I have not even looked at anything form C++11, so I'll be going over this stuff now.

Thanks SOL.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

No problem... and I did side-track the thread, oops. laugh.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 ?


The new C++11 features make C++ easier to use as a beginner, so it's definitely something you should look into and learn piece by piece overtime, in the same way you learn the current version of C++ piece by piece over time.

Any old code won't become non-compliant - it's an extension of the previous standards, without discarding any previous parts.
Because the standard is so new (2011, hence the name), no compiler currently implements every part of the standard, though because working drafts of the standard have been available for years, the major compilers (Microsoft's and GCC/MinGW) already implement the majority of it, and certainly all the parts I mentioned in my post.

Some compilers require you to pass in a certain command-line parameter to 'unlock' the new features. GCC/MinGW requires '-std=c++11' to be passed in. (Presumably when they are 100% sure they've implemented the standard, it'd be turned on by default).
So your compiler may actually already be compliant, but you just accidentally have it turned off or it might be turned off by default. But it's perfectly stable and guaranteed not to change because it's been officially and permanently standardized.

Or, you just might be using an older version of your compiler, and just need to update to the most recent version.
There's no need to update this second, and no harm comes from waiting another year or so... I just wanted to get a a jump on C++11 myself, because the additions are so great, and because I'll need to learn them sooner or later anyway.

Uh, so to simplify:
- Definitely for beginners AND pros
- Old code still works fine, even if you update
- New features are awesome
- Visual Studio and GCC/MinGW have all the new features I mentioned, and more
- You'll want to learn them eventually, but it doesn't have to be now
- But you can use them now if you desire, just update your compiler

Some compilers require you to pass in a certain command-line parameter to 'unlock' the new features. GCC/MinGW requires '-std=c++11' to be passed in.


Just a minor correction here: In GCC versions prior to 4.7 (which is not yet available out of the box for a lot of linux distros), the flag is '-std=c++0x'.
Thanks, I forgot about that. smile.png
Another detail that may be important, is that GCC 4.7 is not compatible with earlier versions on binary level. I got hit by this when I did "mingw-get upgrade", and my development environment stopped working.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

Another detail that may be important, is that GCC 4.7 is not compatible with earlier versions on binary level. I got hit by this when I did "mingw-get upgrade", and my development environment stopped working.

Yes, there was a nasty ABI fart. You would notice it if you expose a std::list in your API and you try to link two modules using that API built with two different versions. The problem was only noticed when Linux distros started using that compiler version in a big way with C++ features enabled (Firefox and Ubuntu's Unity desktop use C++11). The Ogre game framework is affected but does not require C++11 in released versions.

The bug was fixed in 4.7.2 and later (by reverting the C++11-required O(1) size() feature in std::list).

There was also a similar problem in C++ std::string.

Avoid using GCC 4.7.0 or 4.71.

Stephen M. Webb
Professional Free Software Developer


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.


Here's the solution: http://vegardno.blog...jects-in-c.html

Note, that using "std::string::operator<" is not optimal -- however, "std::string" happens to implement a member function "compare", which is better.

The blog post shows a simple perf comparison of the two above-mentioned approaches:
"Running this on my laptop, it takes approximately 3.9 seconds to run the original code and 2.9 seconds to run the new version."

EDIT: Ah, noticed you only need to sort by "points", that's a simpler case; then, I'll only keep this reply in case anyone wanders in here by Google search and tries to do sorting via strings... ;-)

This topic is closed to new replies.

Advertisement