Skip to content

Instantly share code, notes, and snippets.

@duckie
Created February 20, 2013 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckie/4995530 to your computer and use it in GitHub Desktop.
Save duckie/4995530 to your computer and use it in GitHub Desktop.
C++11 : Compute a rank with a map.
#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_,&current_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