【问题标题】:c++ finding the argsort of a vector<float>c++ 查找向量的 argsort<float>
【发布时间】:2020-12-06 14:19:54
【问题描述】:

关注C++ sorting and keeping track of indexes: 这是如何获得vector&lt;int&gt; vargsort

vector<int> VectorArgSort(vector<int> &v) {
    /* Get the indices that gives a sorted vector v*/

    vector<int> retIndices(v.size());
    iota(retIndices.begin(), retIndices.end(), 0);

    stable_sort(
        retIndices.begin(), retIndices.end(),
        [&v](int i1, int i2) {return v[i1] < v[i2];});

    return retIndices;
}

但我想要的是让 vector&lt;int&gt; VectorArgSort(vector&lt;float&gt; &amp;v) 仍然给出向量,因为它们是索引,而是在 vector&lt;float&gt; &amp;v 上执行它。

我尝试将几个已定义的 int 类型更改为双精度类型,但我认为我不应该这样做,因为它们都指向索引。所以我不知道如何改变函数来得到我想要的。

【问题讨论】:

  • 请显示错误信息。

标签: c++ sorting indices


【解决方案1】:

您只需将函数参数中的vector&lt;int&gt; 更改为vector&lt;float&gt;

vector<int> VectorArgSort(vector<float> &v) {

查看工作版本here

【讨论】:

    猜你喜欢
    • 2011-10-12
    • 2019-09-25
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2017-12-22
    相关资源
    最近更新 更多