Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何根据结构/类的成员数据对其数组进行排序?_C++_Arrays_Algorithm_Class_Sorting - Fatal编程技术网

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

C++ 如何根据结构/类的成员数据对其数组进行排序?,c++,arrays,algorithm,class,sorting,C++,Arrays,Algorithm,Class,Sorting,如果失败,如何根据结构/类的成员数据对其数组进行排序 #include <iostream> #include <algorithm> #include <vector> using namespace std; struct O{ const string n; int a=1; }; bool bfunction (O a, O b) { ret

如果失败,如何根据结构/类的成员数据对其数组进行排序

#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);
}

衷心感谢您的帮助

使用
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);
}
#包括
#包括
#包括
#包括
结构O{
std::字符串n;
INTA;
O(const char*c):a(1){n=c;}
};
布尔函数(O a,O b){
返回a.n
此处出现了一些错误:

  • 排序(m.begin()、m.end()、&b函数)
    O[]
    上调用
    begin()
    end()
    。但是数组没有任何成员函数

    您可以在这里进行选择:要么制作
    m
    一个
    std::array
    或者一个
    std::vector
    或者使用
    std::begin(m)
    std::end(m)
    ,这两种方法都适用于静态数组

  • 排序函数应通过常量引用获取其参数:

    bool b功能(常数O&a、常数O&b)

  • 在排序函数中,
    a.n
    比较两个字符串数组,但这种比较没有在任何地方定义。这是一个你需要解决的逻辑错误。想想你到底想比较什么。比较是为
    std::string
    定义的,例如
    返回a.n[0]将起作用

  • 在对任何内容进行排序时,都需要移动元素。但是您的结构
    O
    没有移动构造函数,因为您没有提供移动构造函数,而自动生成的移动构造函数的格式将不正确,因为
    O
    具有
    const
    成员

    我认为处理这个问题的最好方法是使所有成员变量都是私有的,并通过getter和setter控制对它们的访问。目前,最简单的方法是删除
    常量


  • 下面是一个复制粘贴就绪的示例,演示了如何执行此操作(假设您的
    n
    应该只是一个字符串,而不是七个字符串的数组)。如果您真的希望字符串数组为
    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() << ',';
        }
    }
    
    #包括
    
    #包括。

    这是否回答了您的问题?您实际的问题是,
    m
    是一个没有任何函数的
    O[]
    。使用
    std::vector
    。(您尝试在
    O[]
    上调用
    .begin()
    )并在排序函数中比较字符串数组。是否要比较两个字符串?C样式数组没有
    begin()
    end()
    成员。改用
    std::begin()
    std::end()
    (或
    std::array
    )。@churill或使用
    std::begin()
    std::end()
    ,这两种方法可用于固定数组。但是代码仍然会失败,因为
    a.n
    没有为
    string[]
    数组定义。而且,
    bfunction()
    需要通过引用获取其参数。我认为这不会解决实际的排序部分,或者会吗?它会解决编译问题,这是朝着正确方向迈出的一步。是的,
    std::array
    需要两个模板参数,第二个是大小,我打开了VS2019,并验证了代码是否正确构建。编辑了我的答案。谢谢大家!
    #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() << ',';
        }
    }