Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: cout ascii art c++ 
Cpp :: convert c++ to mips 
Cpp :: what c++ library is arccos in 
Cpp :: static member fn , instance 
Cpp :: c++ dynamic array 
Cpp :: beecrowd problem 1001 solution in c++ 
Cpp :: overloading templates in cpp 
Cpp :: gcd multi num 
Cpp :: is variable sized array are not allowed in c++? 
Cpp :: remove digit from number c++ 
Cpp :: delete item from linked list in c++ 
Cpp :: c++ to c code converter online 
Cpp :: contains in c++ map 
Cpp :: what does npl mean? 
Cpp :: convert char to string c++ 
Cpp :: ue4 c++ bool to text 
Cpp :: c++ tuple example 
Cpp :: flowchart to display factors of a number 
Cpp :: C++ Creating a Class Template Object 
Cpp :: how to calculate marks in C++ 
Cpp :: convert c program to c++ online 
Cpp :: multiple inheritance c++ 
Cpp :: No Index Out of Bound Checking in C++ 
Cpp :: 3 conditions for a while loop c++ 
Cpp :: spyder enviroment 
Cpp :: std::throw_with_nested 
Cpp :: cpp console progressbar 
Cpp :: Chef and IPC Certificates codechef solution in c++ 
Cpp :: Required Length 
Cpp :: converter python to c++ code 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =