c++ - Create ranking for vector of double -


i have vector doubles want rank (actually it's vector objects double member called costs). if there unique values or ignore nonunique values there no problem. however, want use average rank nonunique values. furthermore, have found question @ ranks, ignore non-unique values.

example, have (1, 5, 4, 5, 5) corresponding ranks should (1, 4, 2, 4, 4). when ignore non-unique values ranks (1, 3, 2, 4, 5).

when ignoring nonunique values used following:

void population::create_ranks_costs(vector<solution> &pop) {   size_t const n = pop.size();    // create index vector   vector<size_t> index(n);   iota(begin(index), end(index), 0);    sort(begin(index), end(index),         [&pop] (size_t idx, size_t idy) {           return pop[idx].costs() < pop[idy].costs();        });    // store result in corresponding solutions   (size_t idx = 0; idx < n; ++idx)     pop[index[idx]].set_rank_costs(idx + 1); } 

does know how take non-unique values account? prefer using std::algorithm since imo lead clean code.

one way using multimap.

  • place items in multimap mapping objects size_ts (the intial values unimportant). can 1 line (use ctor takes iterators).

  • loop (either plainly or using whatever algorithm) , assign 0, 1, ... values.

  • loop on distinct keys. each distinct key, call equal_range key, , set values average (again, can use stuff algorithm this).

the overall complexity should theta(n log(n)), n length of vector.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -