Created
February 20, 2013 13:23
-
-
Save duckie/4995530 to your computer and use it in GitHub Desktop.
C++11 : Compute a rank with a map.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <map> | |
#include <utility> | |
struct guy | |
{ | |
std::string name_; | |
float mark_; | |
unsigned int rank_ = 0; | |
guy(std::string const & name, float mark) : name_(name), mark_(mark) | |
{} | |
}; | |
int main() | |
{ | |
// Vector construction | |
std::vector<guy> guys; | |
guys.push_back(guy("Roger",16.0f)); | |
guys.push_back(guy("Marcel",10.0f)); | |
guys.push_back(guy("Robert",12.0f)); | |
// Rank computing | |
{ | |
std::map<float, guy*> sorter; | |
for(guy & current_guy : guys) { | |
sorter.insert(std::make_pair(current_guy.mark_,¤t_guy)); | |
} | |
unsigned int rank = guys.size(); | |
for(std::pair<float,guy*> const & elem : sorter) { | |
elem.second->rank_ = rank; | |
--rank; | |
} | |
} | |
// Displays the result | |
for(guy const & current_guy : guys) { | |
std::cout << current_guy.name_ << " is ranked " << current_guy.rank_ << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment