1) Create y as a vector of index (an integer range)
2) Sort this range with a comparator that returns the indexes elements from x
Using the Standard Library, that gives :
#include
#include
#include
int main() {
std::vector x = {15, 3, 0, 20};
std::vector y;
std::vector y(x.size());
std::size_t n(0);
std::generate(std::begin(y), std::end(y), [&]{ return n++; });
std::sort( std::begin(y),
std::end(y),
[&](int i1, int i2) { return x[i1] < x[i2]; } );
for (auto v : y)
std::cout << v << ' ';
return 0;
}
Live demo.