Horje
c++ sorting and keeping track of indexes Code Example
c++ sorting and keeping track of indexes
#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;
}




Cpp

Related
what algorithm does bitcoin use Code Example what algorithm does bitcoin use Code Example
What is a ~ in c++ Code Example What is a ~ in c++ Code Example
new float array c++ Code Example new float array c++ Code Example
check if equal to \ char or not c++ Code Example check if equal to \ char or not c++ Code Example
insertion overloading in c++ Code Example insertion overloading in c++ Code Example

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