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.

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.

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.

The std::sort() algorithm function takes three parameters*. The first parameter is an iterator saying where in the container to begin sorting, the second parameter says at what point to stop sorting, and the third parameter is a callback function (or functor object) that is used to describe how to sort the elements of the container.

A new for() loop style was added by C++, called range-for(). It allows you to sort-hand common iteration over containers and arrays.It takes two parameters seperated by a colon (not a semi-colon). The first parameter is a temporary variable that will hold/reference each element, and the second parameter is a container of elements.

It gets rid of alot of boilerplate code in alot of common situations. There are reasons to use both styles of for() loops, so it doesn't replace the old one, just serves as an additional version in your toolset.

(For the record, it'd be more proper for the variable in the example I gave above to be a reference or const reference, but it doesn't have to be)

for(const std::string &str : myStrings)

The second new feature used in the for-loop was the 'auto' type of variable used for type inference.The type of 'auto' is automatically deduced at compile-time, so it's still as strongly-typed as ever, but is much more convenient in some situations like very long variable names...

//Constructing and initializing a vector iterator the old way:
std::vector< std::pair<bool, std::string> >::iterator myIterator = myVector.begin(); //Woah! That's a mouthful of code, and it serves no purpose either... We just want an iterator.
auto myIterator = myVector.begin(); //Easy! And just as type-safe.

These are all standard parts of modern C++ (The previously named C++0x standard was passed last year successfully, becoming C++11, replacing the C++98 and C++03 standards). Most of them are implemented in the major compilers already, though you may need to tell the compilers to enable them.

The new standard makes C++ faster, safer, more flexible, and funner to work with.

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

Neither chunks of code were commented, and both are simple to advanced C++ users but confusing to newer users - it just happens that the method Bregma used is unfamiliar because it's rather new (and to be fair, he said it was from the new standard, though maybe not as clearly as he intended to). DevLiquidKnight's version would be equally confusing to newer programmers (or at least equally confusing to me when I was a new programmer).

I suggest reading through the new C++11 features - there are alot more. I also strongly recommend the videos of the Going Native 2012 C++ conference - very very informative and interesting.

I'm using a few of them on a regular basis in my code now, and have for several months. I haven't yet taken full advantage of all the new features (I've only used lambdas once or twice so far, mostly from inexperience), and haven't yet learned all the new C++11 standard library additions (like better random number generators, and better smart pointers, and regex capabillities, and more), but I tend to work more and more of C++11 into my coding toolbox gradually. Very cool additions.

My favorite C++11 addition (which I only just got access last night when I updated to the most recent MinGW compiler), is in-class member initialization.

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()".

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 ?

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?

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.

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

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.

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).

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.

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... ;-)