Kon*_*lph 37
使用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;
}
Run Code Online (Sandbox Code Playgroud)
你可以尝试这样的事情:
template<typename C>
class index_sorter {
public:
compare(C const& c) : c(c) {}
bool operator()(std::size_t const& lhs, std::size_t const& rhs) const {
return c[lhs] < c[rhs];
}
private:
C const& c;
};
std::sort(index_vector.begin(), index_vector.end(), index_sorter(vector));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12513 次 |
| 最近记录: |