B
boheman
Hi, I am wondering if there is a simple and quick way to return the
indices of sorted vector.
for example, I have a vector<int> x containing {5, 2, 3, 0, 2}.
I can use
sort(x.begin(), x.end(), less<int>());
to sort the vector x. Then the sorted x becomes {0, 2, 2, 3, 5}. But
how can I obtain the indices of sorted x, which is {3, 1, 4, 2, 0}.
I know that I can insert each element of x paired with the index into a
multimap and read out the reordered indices. But that is not what I am
looking for since I do not want to spend memory to keep another copy of
my vector x in multimap.
Surapong L.
indices of sorted vector.
for example, I have a vector<int> x containing {5, 2, 3, 0, 2}.
I can use
sort(x.begin(), x.end(), less<int>());
to sort the vector x. Then the sorted x becomes {0, 2, 2, 3, 5}. But
how can I obtain the indices of sorted x, which is {3, 1, 4, 2, 0}.
I know that I can insert each element of x paired with the index into a
multimap and read out the reordered indices. But that is not what I am
looking for since I do not want to spend memory to keep another copy of
my vector x in multimap.
Surapong L.