【问题标题】:use std::sort compare function to sort corresponding other vector elements [duplicate]使用 std::sort 比较函数对相应的其他向量元素进行排序[重复]
【发布时间】:2020-10-18 07:34:38
【问题描述】:

我有 2 个向量,一个是 int32_t 类型的向量,它对应于学生的年龄,另一个是 std::string 类型的向量,它对应于学生的姓名。我想根据年龄(降序)对学生进行排序,并在学生姓名列表中反映排序变化。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


void print(const std::vector<int32_t>& student_age_list, const std::vector<std::string>& student_name_lsit) {
    int32_t size = student_age_list.size();
    for(int32_t i = 0; i < size; ++i)
        std::cout << "Name: " << student_name_lsit[i] << ", Age: " << student_age_list[i] << "\n";
    std::cout << std::endl;
}

int32_t main(int32_t argc, char *argv[]) {
    std::vector<int32_t> student_age_list { 23, 56, 34, 77, 23, 66, 54, 34 };
    std::vector<std::string> student_name_list { "abc", "sjdg", "gdagd", "twue", "sgfdah", "tywet", "mbmdas", "uyqwteu" };
    
    print(student_age_list, student_name_list);
    
    std::sort(student_age_list.begin(), student_age_list.end(), [](const int32_t& a, const int32_t& b) { return a > b; });
    
    std::cout << "\nAfter sort\n\n";
    print(student_age_list, student_name_list);
    
    return EXIT_SUCCESS;
}

【问题讨论】:

  • 为什么是 2 个向量?无法将 2 个数据集对应在一起,因此排序一个将与另一个不同步。为什么不使用 1 个向量来保存结构/类类型的元素?这样,学生信息就作为一个数据单元保存在一起。
  • 实际上输入是作为两个单独的列表提供给我的
  • 不过,它不会阻止您将其存储为单个结构向量。
  • 我知道我可以创建另一个向量,例如std::vector&lt;std::pair&lt;int32_t, std::string&gt;&gt;,并根据该对的第一个元素进行排序。但这不必要地消耗了额外的空间。所以只是为了寻找更好的方法
  • @Harry 具有 1 个对向量不使用比相同数据的 2 个单独向量更多的内存。你试图做的事情不能按照你展示的方式完成。您需要将数据合并到 1 个向量中,或者使用第 3 个向量将其他 2 个向量链接在一起。

标签: c++


【解决方案1】:

鉴于您显示的代码,根本无法将年龄数据与姓名数据相关联,因此排序一个向量将与另一个向量不同步。

您应该改用 1 个向量。创建一个struct/class 来保存每个学生的数据,然后为该数据使用 1 个向量。例如:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct StudentInfo
{
    std::string name;
    int32_t age;
};

void print(const std::vector<StudentInfo>& student_list) {
    for(const auto &student : student_list)
        std::cout << "Name: " << student.name << ", Age: " << student.age << "\n";
    std::cout << std::endl;
}

int main(int argc, char *argv[]) {
    std::vector<StudentInfo> student_list { {“abc”,23}, {“sjdg”,56}, {“gdagd”,34}, {“twue”,77}, {“sgfdah”,23}, {“tywet”,66}, {“mbmdas”,54}, {“uyqwteu”,34} };
    
    print(student_list);
    
    std::sort(student_list.begin(), student_list.end(), [](const StudentInfo& a, const StudentInfo& b) { return a.age > b.age; });
    
    std::cout << "\nAfter sort\n\n";
    print(student_list);
    
    return EXIT_SUCCESS;
}

Live Demo

如果这不是您的选择,无论出于何种原因,您都必须以某种方式将 2 个矢量数据链接在一起。例如,您可以使用第 3 个向量将索引保存到其他 2 个向量中,然后对索引进行排序,例如:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void print(const std::vector<int32_t>& student_age_list, const std::vector<std::string>& student_name_list, const std::vector<size_t>& student_index_list) {
    for(size_t index : student_index_list)
        std::cout << "Name: " << student_name_list[index] << ", Age: " << student_age_list[index] << "\n";
    std::cout << std::endl;
}

int main(int argc, char *argv[]) {
    std::vector<int32_t> student_age_list { 23, 56, 34, 77, 23, 66, 54, 34 };
    std::vector<std::string> student_name_list { "abc", "sjdg", "gdagd", "twue", "sgfdah", "tywet", "mbmdas", "uyqwteu" };
    std::vector<size_t> student_index_list { 0, 1, 2, 3, 4, 5, 6, 7 };
    
    print(student_age_list, student_name_list, student_index_list);
    
    std::sort(student_index_list.begin(), student_index_list.end(), [&](const size_t& a, const size_t& b) { return student_age_list[a] > student_age_list[b]; });
    
    std::cout << "\nAfter sort\n\n";
    print(student_age_list, student_name_list, student_index_list);
    
    return EXIT_SUCCESS;
}

Live Demo

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多