Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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_Sorting_Selection - Fatal编程技术网

C++ 类对象的选择排序数组

C++ 类对象的选择排序数组,c++,arrays,sorting,selection,C++,Arrays,Sorting,Selection,我有一个部分有效的选择排序算法,我使用它按年龄对类对象数组进行排序。当它这样做时,它会对一些内容进行正确排序,但无法对第一个元素进行排序。还有一种方法可以对类使用赋值运算符,使其更容易 谢谢 以下是我的完整代码: #include <iostream> #include <string> #include <math.h> using namespace std; void storeinfo() ; void showinfo() ; void menu

我有一个部分有效的选择排序算法,我使用它按年龄对类对象数组进行排序。当它这样做时,它会对一些内容进行正确排序,但无法对第一个元素进行排序。还有一种方法可以对类使用赋值运算符,使其更容易

谢谢

以下是我的完整代码:

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

void storeinfo() ;
void showinfo() ;
void menu() ;
void deleteinfo() ;
void displayallinfo() ;
void selectionSort() ;
int linsearch(string val) ;


class user 
{
    string firstname, lastname, currentteam, position, status ;
    int age ;
public:
    user() {};
    user(string fname, string lname, string cteam, string pos, string stat, int age) 
    {
        setFirstName(fname);
        setLastName(lname);
        setCurrentTeam(cteam);
        setPosition(pos);
        setStatus(stat);
        setAge(age);
    } ;



    user& operator = (const user& source)
    {
        firstname = source.firstname;
        lastname = source.lastname ;
        currentteam = source.currentteam ;
        position = source.position ;
        status = source.status ;
        age = source.age ;
    }


    void setFirstName(string fname)
        {firstname = fname;}
    void setLastName(string lname)
        {lastname = lname;}
    void setCurrentTeam(string cteam)
        {currentteam = cteam;}
    void setPosition(string pos)
        {position = pos;}
    void setStatus(string stat)
        {status = stat;}
    void setAge(int _age)
        {age = _age;}

    string getFirstName()
        {return firstname ;}
    string getLastName()
        {return lastname ;}
    string getCurrentTeam()
        {return currentteam ;}
    string getPosition()
        {return position ;}
    string getStatus()
        {return status ;}
    int getAge()
        {return age ;}
};

user player[20] ;
int arrlength = 3 ;

int main()
{
    menu() ;

    cin.get() ;
    return 0 ;
}

void storeinfo()
{
    string firstname ;
    string lastname ;
    string currentteam ;
    string position;
    string status ;
    int age ;

    for (int i=0; i < 3; i++)
    {
        cout << "\n\n Enter First Name : " ; 
        cin >> firstname ;
        player[i].setFirstName(firstname) ;
        cout << "Enter Last Name : " ; 
        cin >> lastname ;
        player[i].setLastName(lastname) ;
        cout << "Enter Player's Age : " ; 
        cin >> age;
        player[i].setAge(age) ;
        cout << "Enter Current Team : " ; 
        cin >> currentteam ;
        player[i].setCurrentTeam(currentteam) ;
        cout << "Enter Position : " ; 
        cin >> position ;
        player[i].setPosition(position) ;
        cout << "Enter Status : " ; 
        cin >> status ;
        player[i].setStatus(status) ;

        cout << "\n\n\n" ;
    }

    /*cout << string(50, '\n');*/

    menu() ;

}

void showinfo()
{
    string search;
    int found ;


    cout << "Please Enter The Player's Last Name : " ;
    cin >> search ;

    found=linsearch(search);

    if (found==-1)
    {
        cout << "\n There is no player called " << search ;
    }
    else
    {
        cout << "\n First Name : " << player[found].getFirstName() << "\n" << "Last Name : " << player[found].getLastName() <<
            "\n" << "Age : " << player[found].getAge() << "\n" << "Current Team : " << player[found].getCurrentTeam() << 
            "\n" << "Position : " << player[found].getPosition() << "\n" << "Status :  " << player[found].getStatus()  << "\n\n";
    }

    cin.get() ;

    menu() ;

}

void deleteinfo()
{
    int arrlength = 3 ;
    string search ;
    int found ;

    cout << "\n Delete A Player's Information \n\n" ;
    cout << "Please Enter The Player's Last Name : " ;
    cin >> search ;

        found=linsearch(search);

    if (found==-1)
    {
        cout << "\n There is no player called " << search ;
    }
    else
    {
        for (int i=found + 1; i < arrlength; ++i)
        {
            player[i-1].setFirstName(player[i].getFirstName()) ;
            player[i-1].setLastName(player[i].getLastName()) ;
            player[i-1].setAge(player[i].getAge()) ;
            player[i-1].setCurrentTeam(player[i].getCurrentTeam()) ;
            player[i-1].setPosition(player[i].getPosition()) ;
            player[i-1].setStatus(player[i].getStatus()) ;
        }

        --arrlength ;

        cout << "\n Player has been deleted." ;

        player[arrlength].setAge(0) ;
        player[arrlength].setCurrentTeam("") ;
        player[arrlength].setFirstName("") ;
        player[arrlength].setLastName("") ;
        player[arrlength].setPosition("") ;
        player[arrlength].setStatus("");
    }

    cin.get() ;

    menu() ;
}

void displayallinfo()
{

    selectionSort();

    for (int i=0; i < 3; i++)
    {
        cout << "\n First Name : " << player[i].getFirstName() << "\n" << "Last Name : " << player[i].getLastName() <<
            "\n" << "Age : " << player[i].getAge() << "\n" << "Current Team : " << player[i].getCurrentTeam() << 
            "\n" << "Position : " << player[i].getPosition() << "\n" << "Status :  " << player[i].getStatus()  << "\n\n";
    }

    cin.get() ;

    menu() ;
}

void menu()
{
    cout << "\n\n MENU" << "\n" ;
    cout << "\n A. Store Player Information" ;
    cout << "\n B. Show Player Informaton" ;
    cout << "\n C. Delete Player Information" ;
    cout << "\n D. Display All Players Sorted By Age";
    cout << "\n Z. Exit \n\n" ;

    string x =  "";
    cin >> x ;

    if (x=="a" | x=="A")
    { 
        storeinfo() ;
    }
    else if (x=="b" | x=="B")
    {
        showinfo() ;
    }
    else if (x=="c" | x=="C")
    {
        deleteinfo() ;
    }
    else if (x=="d" | x=="D")
    {
        displayallinfo() ;
    }
    else if (x=="z" | x=="Z")
    {
        exit(0) ;
    }
    else
    {
        cout << "Invalid Choice" ;
        menu() ;
    }
}

int linsearch(string val)
{
    for (int j=0; j <= 3; j++)
    {
        if  (player[j].getLastName()==val)
         return j ;         
    }
        return -1 ;
}

void selectionSort()
{
    int i, minIndex, minValue;
    for (i = 0; i < (arrlength - 1); i++)
    {
        minIndex = i ;
        minValue = player[i].getAge() ;
        for (int index = i + 1; index < arrlength; index++)
        {
            if (player[index].getAge() < minValue)
            {
                minValue = player[index].getAge();
                minIndex = index;

            }
        }

        player[minIndex].setAge(player[i].getAge());
        player[i].getAge() == minValue;

    }



}
#包括
#包括
#包括
使用名称空间std;
void storeinfo();
void showinfo();
无效菜单();
void deleteinfo();
void displayallinfo();
void selectionSort();
int-linsearch(字符串val);
类用户
{
字符串firstname、lastname、currentteam、position、status;
智力年龄;
公众:
user(){};
用户(字符串fname、字符串lname、字符串cteam、字符串pos、字符串stat、int age)
{
setFirstName(fname);
setLastName(lname);
setCurrentTeam(cteam);
设置位置(pos);
设置状态(status);
设置(年龄);
} ;
用户和运算符=(常量用户和源)
{
firstname=source.firstname;
lastname=source.lastname;
currentteam=source.currentteam;
位置=源位置;
status=source.status;
年龄=来源。年龄;
}
void setFirstName(字符串fname)
{firstname=fname;}
void setLastName(字符串lname)
{lastname=lname;}
void setCurrentTeam(字符串cteam)
{currentteam=cteam;}
无效设置位置(字符串位置)
{position=pos;}
无效设置状态(字符串状态)
{status=stat;}
无效设置(内部设置)
{age=_age;}
字符串getFirstName()
{返回firstname;}
字符串getLastName()
{返回lastname;}
字符串getCurrentTeam()
{返回currentteam;}
字符串getPosition()
{返回位置;}
字符串getStatus()
{返回状态;}
int getAge()
{返回年龄;}
};
用户播放器[20];
int arrlength=3;
int main()
{
菜单();
cin.get();
返回0;
}
void storeinfo()
{
字符串名;
字符串lastname;
字符串组;
串位置;
字符串状态;
智力年龄;
对于(int i=0;i<3;i++)
{
cout>名字;
player[i].setFirstName(firstname);
cout>lastname;
player[i].setLastName(lastname);
年龄;
玩家[i]。设置年龄(年龄);
cout>currentteam;
玩家[i].setCurrentTeam(currentteam);
cout>位置;
游戏者[i]。设置位置(位置);
状态;
玩家[i].setStatus(状态);
cout您的“user”类非常简单,不需要重载赋值运算符,因为它的所有成员都是普通的旧数据类型,或者已经有了自己的赋值运算符(如string)我建议从这个类中移除操作符=方法,这样当你添加新成员时,你就没有维护操作符=方法的维护。如果需要的话,C++会自动为你的类生成赋值运算符(这是成员分配)。.当您的成员是指针时,您实际上只需要一个重载赋值运算符,但我离题了

您的选择排序功能实际上并不是对玩家进行排序。它只是重新排列每个玩家的“年龄”值。我认为这就是您要对“玩家”数组进行实际“排序”的操作。(请注意,这比“C++”更像是“C”,但请耐心等待)

void SimpleButSlowSort()
{
boolfsorted=false;
if(arrLength播放器[index+1].getAge())
{
用户温度;
temp=玩家[索引];
玩家[索引]=玩家[索引+1];
玩家[索引+1]=临时;
fSorted=false;
}   
}
}
}
<>但是如果你想要一个C++标准的排序方法,你可以使用标准库来进行排序。但这需要玩家的类型可以被迭代。范例

bool MyPlayerCompare(user& u1, user& u2) { return (u1.getAge() < u2.getAge()); }
std::vector<user> players;
void FasterSort()
{
    std::sort(players.begin(), players.end(), MyPlayerCompare);

}
bool MyPlayerCompare(user&u1,user&u2){return(u1.getAge()
为什么我会得到负面评价?如果你看到这个问题,告诉我我做错了什么,或者给我指出了正确的方向,至少要留下评论。即使我没有投反对票,我也很乐意发表评论。因此,这是一个特定的、个人编程问题的家。我不明白你的一个问题是什么。当我猜你的一个问题是什么的时候,我觉得你并没有表现出对具体问题的努力。“这是我的整个计划,你找出问题所在”这样的问题似乎在这里得到了压倒性的票数。试着问一个简单明了的问题,用最小的示例代码来表达你的问题。这将表明你也希望努力得到答案。谢谢我采纳了你的c策略并实施了它,它成功了,再次感谢
bool MyPlayerCompare(user& u1, user& u2) { return (u1.getAge() < u2.getAge()); }
std::vector<user> players;
void FasterSort()
{
    std::sort(players.begin(), players.end(), MyPlayerCompare);

}