template <typename T>
void vec_print(string vec_name,vector<T> &vec ){
cout<<vec_name<<".size()="<<vec.size()<<endl;
cout<<vec_name<<":\n"<<endl;
for (auto val : vec){
cout << val << " ";
}
cout<<endl;
}
template <typename T>
vector<int> argsort(const std::vector<T>& array)
{
const int array_len(array.size());
std::vector<int> array_index(array_len, 0);
for (int i = 0; i < array_len; ++i)
array_index[i] = i;
std::sort(array_index.begin(), array_index.end(),
[&array](int pos1, int pos2) {return (array[pos1] < array[pos2]);});
return array_index;
}
vec_print("vec_peak_heights", vec_peak_heights );
vector<int> sort_index = argsort(vec_peak_heights);
vec_print("vec_peak_heights", vec_peak_heights );
vec_print("sort_index", sort_index );
#result:
vec_peak_heights.size()=54
vec_peak_heights:
1122 1255 1030 1070 1347 1232 1196 1313 1169 1265 1240 1326 1301 1216 1140 1298 1384 1281 1304 1239 1503 1294 1240 1472 1484 1528 1485 554 1265 1094 1397 1095 1318 1272 1414 1209 1265 1380 1463 1427 1570 1623 1538 1415 1299 1469 1306 1504 1280 1074 821 367 255 355
sort_index.size()=54
sort_index:
52 53 51 27 50 2 3 49 29 31 0 14 8 6 35 13 5 19 22 10 1 9 36 28 33 48 17 21 15 44 12 18 46 7 32 11 4 37 16 30 34 43 39 38 45 23 24 26 20 47 25 42 40 41
sorted values:
255 355 367 554 821 1030 1070 1074 1094 1095 1122 1140 1169 1196 1209 1216 1232 1239 1240 1240 1255 1265 1265 1265 1272 1280 1281 1294 1298 1299 1301 1304 1306 1313 1318 1326 1347 1380 1384 1397 1414 1415 1427 1463 1469 1472 1484 1485 1503 1504 1528 1538 1570 1623
v