Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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++_Sorting_Struct - Fatal编程技术网

对结构向量排序 我有一个大问题…我是一个编程新手,我试着在C++中为足球联赛做一个程序,根据他们的观点对球队进行排序。

对结构向量排序 我有一个大问题…我是一个编程新手,我试着在C++中为足球联赛做一个程序,根据他们的观点对球队进行排序。,c++,sorting,struct,C++,Sorting,Struct,有人能帮个忙吗 我为团队创建了一个结构,包含名称和点数 我如何对队伍进行分类?另外,对不起,我的英语不好 这是我的代码: #include <iostream> #include <algorithm> using namespace std; //I created a struct for the team. struct team { char name; int pct; }v[20]; int main() { int i,sw,aux

有人能帮个忙吗

我为团队创建了一个结构,包含名称和点数

我如何对队伍进行分类?另外,对不起,我的英语不好

这是我的代码:

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

//I created a struct for the team.

struct team
{
    char name;
    int pct;
}v[20];

int main()
{   int i,sw,aux;

    for(i=1;i<=4;i++)//read the names
    {   
        cout<<"Team "<<i<<endl;
        cin>>v[i].name;
    }

    for(i=1;i<=4;i++)//get the points
    {
        cout<<"Team "<<v[i].name<<" points"<<endl;
        cin>>v[i].pct;
    }

    //bubble sort(not working)
    do
    {
        sw=0;
        for(i=1;i<=4;i++)
        {
            if(v[i].pct<v[i+1].pct)
            aux=v[i].pct;
            v[i].pct=v[i+1].pct;
            v[i+1].pct=aux;
            sw=1;
        }
    }while(sw==1);

    for(i=1;i<=4;i++)
    {
        cout<<v[i].pct<<endl;
    }

    return 0;
}

您需要像这样修改排序部分。假设您是按desc顺序排序的

do
{
    sw=0;
    for(i=1;i<4;i++) //< not <= ,because in case of the last element you wont have any other element after it to compare
    {
        if(v[i].pct<v[i+1].pct) // use curly brace as you want all 4 following lines to be executed when its true
        {
            aux=v[i]; //swap entire struct not just one variable
            v[i]=v[i+1];
            v[i+1]=aux;
            sw=1;
        }
    }
}while(sw==1);
此外,您可能需要编辑团队名称的变量类型,因为它可以是字符串

当你使用C++时,你可以使用一个线性函数来排序

//#include<algorithm>
//define comparator function
bool cmp(team a, team b)
{
    return a.pct < b.pct;
}

sort(v+1,v+4+1,cmp);
您也可以简单地将比较器写入结构中,然后使用排序函数:

struct team
{
    char name;
    int pct;

    bool operator<(team other) const
    {
        return pct > other.pct;
    }
}v[20];
sort(v+1,v+4+1);

我很好奇为什么你包括算法,但不使用它的任何一个。你知道STL吗?由于您包含了算法,我想您可能知道一些简单的函数,例如交换、排序和复制。它们很容易使用,你只需要键入一行,而不是自己写一个冒泡排序。在使用sort函数之前,您应该定义哪些顺序可以对这些团队生效。就这样,

bool compareTeams(const Team &t1, const Team &t2) {
    if (t1.getScore() == t2.getScore()) {
        return t1.getName() < t2.getName();
    }
    else {
        return t1.getScore() < t2.getScore();
    }
}

这段代码是不可能读懂的。你自己做吗?为什么不可能?是的,我做了。你需要交换整个元素,而不仅仅是它们的点。您还需要更加注意数组边界。嗯,冒泡排序不起作用。我正在搜索另一个解决方案。您的团队名称存储为1个字符。如果这不是你的意图,你必须创建一个字符数组,或者最好使用std::string.my bad我忘了删除include..我不知道排序函数是如何工作的。是的,你是对的。
std::sort(v, v + 20, compareTeams);