c++ - 如何对在私有(private)中声明的整数进行排序

标签 c++ arrays sorting

我在类(class)的私有(private)部分声明了 int id。

                    private:
                      int id,age;
                      float gpa;
                      string last,first;

这段代码在我的文件中,用于显示和调用数组中的函数,并对 int id 进行排序。

        student[i].sort_array(student[i].id,cap);
        i++;
        cout << i;

这是一个单独的文件,我把我的函数放在那里,如果我 student[i].put(cout) 数据,我能够显示数组的内容。我不确定如何传入一个整数,该整数会出现在我类(class)的证明部分

        void student::sort_array(int student[i].get(id),int n)
        {

            int j,temp;
            for(j=0;j<n-1;j++)
            { 
         //if out of position switch the out of align number
            if(student[j]<student[j+1])
            {
            temp =  student[j];
            student[j] = student[j+1];
            student[j+1] = temp;
            }
         }

最佳答案

正常的方法是有一个bool student::compareAges(Student const& otherStudent)方法,并将其作为额外参数传递给比较函数。例如。这是 std::sort 的第三个参数当你不使用默认 operator<

关于c++ - 如何对在私有(private)中声明的整数进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10396527/

相关文章:

c++ - 为什么这不能在 Code::Blocks 中用 mingw 编译?

c# - 如何定义一个 26x2 数组然后在其行上使用 LINQ?

bash - 如何使用 awk 按列的最后一个值对列进行排序?

java - 在 java 中按列标题对二维列表进行排序

c++ - std::array 大括号如何初始化?

c++ - Visual Studio 2013 fatal error C1041/FS

php - 使用 PHP 和 MySQL 创建多级 JSON

c++ - 字符串插入排序

c++ - Mac 和 Linux 机器之间正则表达式的兼容性问题

c++ - 我的 KD 树有什么问题? (K=2)