C ++排序跟踪指数 [英] c++ sort keeping track of indices

查看:121
本文介绍了C ++排序跟踪指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有一些有效的例程用于返回数组指数在一个数组排序的元素呢?我认为,一些方便的方式中存在使用STL的载体。你已经实现了一个高效的算法中没有STL,或者你有一个裁判伪code或C ++ code?

do you have some efficient routine for returning array with indices for sorted elements in a array? I think that some convenient way exists using stl vector. Do you have already implemented an efficient algo without stl, or do you have a ref to pseudo code or C++ code?

感谢和问候

推荐答案

使用C ++ 11,下面的应该只是罚款:

Using C++11, the following should work just fine:

template <typename T>
std::vector<size_t> ordered(std::vector<T> const& values) {
    std::vector<size_t> indices(values.size());
    std::iota(begin(indices), end(indices), static_cast<size_t>(0));

    std::sort(
        begin(indices), end(indices),
        [&](size_t a, size_t b) { return values[a] < values[b]; }
    );
    return indices;
}

这篇关于C ++排序跟踪指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆