【问题标题】:How to sort a vector containing objects of class in C++? [duplicate]如何在 C++ 中对包含类对象的向量进行排序? [复制]
【发布时间】:2021-07-05 08:56:03
【问题描述】:

class(staff) 包含对象 int ID, string Name, string Class 向量包含

vector <staff> s = { {234, "Mark", "biology"},
{3455, "Mitch", "English"},
{1234, "Hen", "Maths"}}

如何根据 ID 对其进行排序?并打印排序? 谢谢

【问题讨论】:

  • 请用您尝试过的代码更新您的问题。

标签: c++ c++11 visual-c++ c++14 c++17


【解决方案1】:

我确信这个问题之前已经回答过了,但是针对您的具体情况;

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

struct staff {
    int ID;
    std::string Name;
    std::string Class;
};

int main() {
    std::vector <staff> s = { 
        { 234, "Mark", "biology" },
        { 3455, "Mitch", "English" }, 
        { 1234, "Hen", "Maths" }
    };

    std::sort(s.begin(), s.end(), []( const auto& a, const auto& b) { return a.ID < b.ID; });

    for (const auto& staff_member : s) 
        std::cout << staff_member.ID << ": " << staff_member.Name << ", " << staff_member.Class << "\n";

    return 0;
}

这使用来自&lt;algorithm&gt; 标头的std::sort 算法,当a 被认为小于b 时,使用返回true 的比较函数调用。

【讨论】:

    【解决方案2】:

    STL 提供std::sort 对容器进行分类(有关更多信息,请参阅here)。 它利用operator&lt; 对容器内的元素进行排序,您可以在其中指定用于排序的元素。

    调用std::sort 后,容器已排序,您可以通过对其进行迭代来打印它。

    这是一个快速而完整的示例:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    class staff {
     public:
      explicit staff(const uint32_t id, const std::string& name, 
          const std::string& class_type) 
        : id_(id), name_(name), class_(class_type) {}
      
      bool operator<(const staff& other) {
          return id_ < other.id_; // sort by id
      }
      
      void print() const {
          std::cout << "ID: " << id_ 
                    << ", name: " << name_ 
                    << ", class: " << class_ << "\n";
      }
      
     private:
      uint32_t id_;
      std::string name_;
      std::string class_;
    };
    
    static void print_staffs(const std::vector<staff>& staffs) {
        for (const staff& staff : staffs) {
            staff.print();
        }
        std::cout << "----------\n";
    }
    
    int main()
    {    
      std::vector<staff> staffs = { staff(234, "Mark", "biology"),
                                    staff(3455, "Mitch", "English"),
                                    staff(1234, "Hen", "Maths") };
       
      print_staffs(staffs);                         // print unsorted               
      std::sort(staffs.begin(), staffs.end());      // sort
      print_staffs(staffs);                         // print sorted 
            
      return 0;
    }
    

    这会产生:

    ID: 234, name: Mark, class: biology                                                                                                                                                                                                                                                                                            
    ID: 3455, name: Mitch, class: English                                                                                                                                                                                                                                                                                          
    ID: 1234, name: Hen, class: Maths                                                                                                                                                                                                                                                                                              
    ----------                                                                                                                                                                                                                                                                                                                     
    ID: 234, name: Mark, class: biology                                                                                                                                                                                                                                                                                            
    ID: 1234, name: Hen, class: Maths                                                                                                                                                                                                                                                                                              
    ID: 3455, name: Mitch, class: English 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 2011-07-17
      • 2011-03-25
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多