• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
rherm23

Sorting Vector containers in C++

17 posts in this topic

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

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1341883765' post='4957470']
[code]

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

[/code]
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='rherm23' timestamp='1341877477' post='4957453']
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.
[/quote]

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:[list]
[*]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.
[/list] Edited by larspensjo
1

Share this post


Link to post
Share on other sites
[img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img] 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 ?
0

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?
0

Share this post


Link to post
Share on other sites
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. Edited by way2lazy2care
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
No problem... and I did side-track the thread, oops. [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

[quote name='ASnogarD' timestamp='1341920929' post='4957587']
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 ?
[/quote]

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 [i]can[/i] use them now if you desire, just update your compiler Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1341940455' post='4957664']
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.
[/quote]

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'.
1

Share this post


Link to post
Share on other sites
Thanks, I forgot about that. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='larspensjo' timestamp='1342002226' post='4957967']
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.
[/quote]
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.
0

Share this post


Link to post
Share on other sites
[quote name='rherm23' timestamp='1341877477' post='4957453']
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.
[/quote]

Here's the solution: [url="http://vegardno.blogspot.com/2012/07/comparing-objects-in-c.html"]http://vegardno.blog...jects-in-c.html[/url]

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... ;-) Edited by Matt-D
0

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  
Followers 0