首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++,基于另一个向量排序

C++,基于另一个向量排序
EN

Stack Overflow用户
提问于 2016-05-21 22:54:11
回答 7查看 24.3K关注 0票数 33

我得到的最好的例子是,我想根据名字的分数对它们进行排序。

代码语言:javascript
复制
vector <string> Names {"Karl", "Martin", "Paul", "Jennie"};
vector <int> Score{45, 5, 14, 24};

因此,如果我将分数排序为{5、14、24、45},则还应该根据它们的分数对名称进行排序。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2016-05-22 02:07:50

正如其他答案中已经提到的:将每个人的名字和分数结合起来可能是最简单的解决方案。

一般说来,这可以通过有时被称为"zip“操作来实现:将两个向量组合成一个对向量--以及相应的”解压缩“。

一般说来,这可能如下所示:

代码语言:javascript
复制
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>

// Fill the zipped vector with pairs consisting of the
// corresponding elements of a and b. (This assumes 
// that the vectors have equal length)
template <typename A, typename B>
void zip(
    const std::vector<A> &a, 
    const std::vector<B> &b, 
    std::vector<std::pair<A,B>> &zipped)
{
    for(size_t i=0; i<a.size(); ++i)
    {
        zipped.push_back(std::make_pair(a[i], b[i]));
    }
}

// Write the first and second element of the pairs in 
// the given zipped vector into a and b. (This assumes 
// that the vectors have equal length)
template <typename A, typename B>
void unzip(
    const std::vector<std::pair<A, B>> &zipped, 
    std::vector<A> &a, 
    std::vector<B> &b)
{
    for(size_t i=0; i<a.size(); i++)
    {
        a[i] = zipped[i].first;
        b[i] = zipped[i].second;
    }
}


int main(int argc, char* argv[])
{
    std::vector<std::string> names {"Karl", "Martin", "Paul", "Jennie"};
    std::vector<int> score {45, 5, 14, 24};

    // Zip the vectors together
    std::vector<std::pair<std::string,int>> zipped;
    zip(names, score, zipped);

    // Sort the vector of pairs
    std::sort(std::begin(zipped), std::end(zipped), 
        [&](const auto& a, const auto& b)
        {
            return a.second > b.second;
        });

    // Write the sorted pairs back to the original vectors
    unzip(zipped, names, score);

    for(size_t i=0; i<names.size(); i++)
    {
        std::cout << names[i] << " : " << score[i] << std::endl;
    }
    return 0;
}
票数 16
EN

Stack Overflow用户

发布于 2019-11-29 21:34:51

将姓名和分数合并为单一结构的另一种方法是创建索引列表并对其进行排序:

代码语言:javascript
复制
 std::vector<int> indices(Names.size());
 std::iota(indices.begin(), indices.end(), 0);
 std::sort(indices.begin(), indices.end(),
           [&](int A, int B) -> bool {
                return Score[A] < Score[B];
            });

现在可以使用indices按所需的排序顺序对NamesScores进行索引。

票数 32
EN

Stack Overflow用户

发布于 2016-05-21 23:06:40

最好的方法是有一个结构,把名字和分数结合起来,并有一个向量。

代码语言:javascript
复制
struct Person
{
    std::string Name;
    int Score;
};

然后你可以声明你的向量:

代码语言:javascript
复制
std::vector<Person> people{ { "Karl", 45 }, { "Martin", 5 }, { "Paul", 14 } };

使用std::sort<algorithm>中进行排序很容易

代码语言:javascript
复制
std::sort(people.begin(), people.end(), 
               [](const auto& i, const auto& j) { return i.Score < j.Score; } );

如果要按降序排序,也可以更改lambda:

代码语言:javascript
复制
std::sort(people.begin(), people.end(), 
               [](const auto& i, const auto& j) { return i.Score > j.Score; } );
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37368787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档