如何排序是在私人声明的整数 [英] how to sort an integer that is declared in the private

查看:97
本文介绍了如何排序是在私人声明的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宣布我的类的私有,部分INT ID。

 私人:
                      INT ID,年龄;
                      浮GPA;
                      去年串,第一;

这code是在我的文件中显示,并呼吁那些在数组中的功能,并进行排序INT ID。

 学生[I] .sort_array(学生[I] .ID,帽);
        我++;
        COUT<<一世;

这是在一个单独的文件,其中我把我的功能,我能够显示数组的内容,如果我的学生[I]。把(COUT)的数据。我不知道如何在一个整数,这将是我班上的provate部分

 无效学生:: sort_array(INT学生[I]获得(ID),INT N)
        {            诠释J,温度;
            为(J = 0; J< N-1; J ++)
            {
         //如果失去位置切换出来对齐数
            如果(学生[J]<学生[D + 1])
            {
            TEMP =学生[J]。
            学生[J] =学生[J + 1];
            学生[J + 1] =温度;
            }
         }


解决方案

的通常方法是有一个布尔学生:: compareAges(学生常量和放大器; otherStudent)方法, a和传递,作为一个额外的参数到您的比较功能。例如。它的第三个参数的std ::排序当你不使用默认的运营商的LT;

I declared the int id in the private, part of my class.

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

This code is in my file to display and call for functions that are in the array, and to sort the int id.

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

This is in a seperate file where i put my functions, i am able to display the contents of the array, if i student[i].put(cout) the data. I am not sure how to pass in an integer that would be in the provate part of my 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;
            }
         }

解决方案

The normal method is to have a bool student::compareAges(Student const& otherStudent) method, aand pass that as an extra argument to your comparison function. E.g. it's the third argument to std::sort when you're not using the default operator<

这篇关于如何排序是在私人声明的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆