Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualMatt-D

Posted 11 July 2012 - 11:20 AM

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

#2Matt-D

Posted 11 July 2012 - 11:18 AM

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

#1Matt-D

Posted 11 July 2012 - 11:17 AM

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.blogspot.com/2012/07/comparing-objects-in-c.html

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

The blog post has done 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."

PARTNERS