-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstring_argsort.cpp
44 lines (37 loc) · 992 Bytes
/
string_argsort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <algorithm> //sort
#include <numeric> //iota
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
sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1].compare(v[i2]) < 0;});
return idx;
}
int main()
{
vector<string> v = {"happy", "ABC", "f", "DE", "a", "harry"};
vector<size_t> ixs = sort_indexes(v);
//happy ABC f DE a harry
for(string e : v){
cout << e << " ";
}
cout << endl;
/**
* ABC, original 1 th
DE, original 3 th
a, original 4 th
f, original 2 th
happy, original 0 th
harry, original 5 th
**/
for(int i = 0; i < (int)v.size(); i++){
cout << v[ixs[i]] << ", original " << ixs[i] << " th" << endl;
}
return 0;
}