1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
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("JeanPaul",12.0f));
guys.push_back(guy("Marcel",10.0f));
guys.push_back(guy("Robert",12.0f));
// Rank computing
{
std::vector<guy*> sorter;
for(guy & current_guy : guys) sorter.push_back(¤t_guy);
std::sort(sorter.begin(), sorter.end(), [](guy* g1, guy* g2){ return g1->mark_ > g2->mark_; });
float prev_mark = 0.f;
unsigned int rank = 0;
for(guy* pguy : sorter)
{
if(pguy->mark_ != prev_mark) ++rank;
pguy->rank_ = rank;
prev_mark = pguy->mark_;
}
}
// Displays the result
for(guy const & current_guy : guys) {
std::cout << current_guy.name_ << " is ranked " << current_guy.rank_ << std::endl;
}
return 0;
} |
Partager