您是否有一些有效的例程来返回带有数组中排序元素索引的数组?我认为使用 stl 存在一些方便的方法vector
。您是否已经在不使用 stl 的情况下实现了高效的算法,或者您是否有伪代码或 C++ 代码的参考?
使用 C++11,以下内容应该可以正常工作:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)