简体   繁体   English

如何根据成员数据对结构/类数组进行排序?

[英]How to sort an array of struct/class based on its member data?

How to sort an array of struct/class based on its member data, as this fails ?如何根据成员数据对结构/类数组进行排序,因为这失败了?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;                                          

struct O{
    const string n;
    int a=1;
};

bool bfunction (O a, O b) {
    return a.n < b.n; }

int main () {

    O m[]={ {"unta"}, {"jalan"}, {"sama"}, {"aki"} };


// using function in sort control
    sort (m.begin(), m.end(), &bfunction);
}

gcc gives: gcc 给出:

 error: request for member ‘begin’ in ‘m’, which is of non-class type ‘O [4]’
     sort (m.begin(), m.end(), &bfunction);
             ^~~~~
 error: request for member ‘end’ in ‘m’, which is of non-class type ‘O [4]’
     sort (m.begin(), m.end(), &bfunction);
                        ^~~~~

sincere useful help is appreciated真诚有用的帮助表示赞赏

Use std::array使用std::array

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>    

struct O {
    std::string n;
    int a;

    O(const char* c) : a(1) { n = c; }
};

bool bfunction(O a, O b) {
    return a.n < b.n;
}

int main() {
    std::array<O, 4> m = { "unta", "jalan", "sama", "aki" };

    // using function in sort control
    std::sort(m.begin(), m.end(), &bfunction);
}

Some mistakes are made here:这里犯了一些错误:

  1. sort (m.begin(), m.end(), &bfunction); calls begin() and end() on an O[] .O[]上调用begin()end() But an array has no member functions whatsoever.但是数组没有任何成员函数。

    You got some choice here: Either make m an std::array<O, 4> or a std::vector<O> or use std::begin(m) and std::end(m) which work for static arrays.您在这里有一些选择:要么使m成为std::array<O, 4>std::vector<O>或使用适用于静态数组的std::begin(m)std::end(m) .

  2. The sorting function should take it's parameters via const reference:排序函数应该通过常量引用来获取它的参数:

    bool bfunction (const O &a, const O &b)

  3. In the sorting function an < bn compares two arrays of strings, but such a comparison is not defined anywhere.在排序函数中, an < bn比较两个字符串数组,但在任何地方都没有定义这样的比较。 Thats a logic error you need to solve.这是需要解决的逻辑错误。 Think about what you actually want to compare here.想想你真正想在这里比较什么。 Comparison is defined for std::string , for example return an[0] < bn[0];比较是为std::string定义的,例如return an[0] < bn[0]; would work.会工作。

  4. When sorting anything elements need to be moved around.排序任何元素时都需要移动。 But your struct O has no move constructor, because you don't provide one and the automatically generated one would be ill formed because O has const members.但是您的 struct O没有移动构造函数,因为您没有提供一个,并且自动生成的构造函数会格式错误,因为O具有const成员。

    I think the best way to deal with this is to make all member variables private and control access to them via getters and setters.我认为解决这个问题的最好方法是将所有成员变量设为私有并通过 getter 和 setter 控制对它们的访问。 For now, the easiest way is to just remove the const .目前,最简单的方法是删除const

Here is a copy-paste-ready example of how I'd do it (assuming your n should simply be a string, rather than an array of seven strings).这是我如何做的一个可复制粘贴的示例(假设您的n应该只是一个字符串,而不是一个包含七个字符串的数组)。 If you really want to have an array of strings as n , then you have to define a proper ordering for them.如果你真的想要一个字符串数组作为n ,那么你必须为它们定义一个正确的顺序。

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>

class O {
    std::string n;
    int a;

public: 

    O(const char* c, int a = 1) : n(c), a(a) {}

    const std::string& get_n() const { return n; }

};

bool bfunction(const O& a, const O& b) {
    return a.get_n() < b.get_n();
}

int main() {

    std::array<O, 4> m = { "unta", "jalan", "sama", "aki" };

    std::sort(m.begin(), m.end(), &bfunction);

    for(auto& x : m) { 
        std::cout << x.get_n() << ',';
    }
}

Live code here .现场代码在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从其成员类/结构之一中访问 class 的成员数据? - How to reach the member data of a class from inside one of its member class/struct? (C++)数组作为结构的成员:如何获得它的大小? - (C++) Array as member of struct: how to get its size? 按结构的给定成员对结构数组进行排序(快速排序) - Sort an array of struct by a given member of the struct (quick sort) 如何通过其不同的属性对对象的 CLASS (不是结构)进行排序? - how to sort A CLASS (not a struct) of objects by its different attributes? 类数据成员struct:访问struct成员 - Class data member struct : accessing struct members 如何在 C++ class 或结构中声明多维数组成员? - How to declare a multidimensional array member in a C++ class or struct? 如何正确地将值赋给具有类数据类型的结构的成员? - How to properly assign a value to the member of a struct that has a class data type? 如何在 class 中定义构造函数,并将结构作为 c++ 中的数据成员? - How to define constructor in class with struct as data member in c++? 如何访问类中结构的数据成员? - How would I access the data member of a struct within a class? 如何使用空值初始化结构的数据成员数组? - how to initialize data member array of a struct with null values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM