📜  c++ 排序和跟踪索引 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:55.730000             🧑  作者: Mango

代码示例1
#include 
#include 
#include       // std::iota
#include     // std::sort, std::stable_sort

using namespace std;

template 
vector sort_indexes(const vector &v) {

  // initialize original index locations
  vector 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;
}