Horje
argsort c++ Code Example
argsort c++
#include <iostream>
#include <vector>
#include <numeric>      // std::iota
#include <algorithm>    // std::sort, std::stable_sort

using namespace std;

template <typename T>
vector<size_t> sort_indexes(const vector<T> &v) {

  // initialize original index locations
  vector<size_t> idx(v.size());
  iota(idx.begin(), idx.end(), 0);

  // sort indexes based on comparing values in v
  // using std::stable_sort instead of std::sort
  // to avoid unnecessary index re-orderings
  // when v contains elements of equal values 
  stable_sort(idx.begin(), idx.end(),
       [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});

  return idx;
}

//Usage:
for (auto i: sort_indexes(v)) {
  cout << v[i] << endl;
}




Cpp

Related
cpp absolute value Code Example cpp absolute value Code Example
round function in c++ Code Example round function in c++ Code Example
compare two functions in a class c++ Code Example compare two functions in a class c++ Code Example
c++ define array with values Code Example c++ define array with values Code Example
c++ set value to inf Code Example c++ set value to inf Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9