How to sort and rank a vector in C++ (without using C++11)

前端 未结 5 879
一个人的身影
一个人的身影 2021-01-21 19:07

I am trying to construct a function take takes a vector, ranks it, sorts it and outputs the sorted and ranked vector with the original positioning of the values. For example: I

5条回答
  •  盖世英雄少女心
    2021-01-21 19:50

    Here is my codes using STL to achieve this in a concise way to get the rank.

    template 
    vector calRank(const vector & var) {
        vector result(var.size(),0);
        //sorted index
        vector indx(var.size());
        iota(indx.begin(),indx.end(),0);
        sort(indx.begin(),indx.end(),[&var](int i1, int i2){return var[i1]

提交回复
热议问题