C++ 按字母顺序排列名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20457270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:09:26  来源:igfitidea点击:

Sort names alphabetically

c++arrayssortingasciialphabetical

提问by Silver Falcon

I'm trying to sort names alphabetically e.g If user enters names and GPA:

我正在尝试按字母顺序对姓名进行排序,例如,如果用户输入姓名和 GPA:

Names          GPA
Peter          2.8
Robert         5.6
David          7.8

The output should be : -

输出应该是: -

Names          GPA
David          7.8
Peter          2.8
Robert         5.6

Here is my program so far (INCOMPLETE):-

这是我的计划至今(未完成): -

#include <iostream>
using namespace std;

int main()
{
    char name [5][25];
    float gpa [5];
    int i;

    for (i=0 ; i<5 ; i++)
    {
        cout << "Enter name " << i+1 << "  :   ";
        cin >> name [i];
        cout << "Enter GPA     :   ";
        cin >> gpa [i];
        cout << endl;
    }

    cout << "\n********** Your entered data **********\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    for (i=0 ; i<5 ; i++)
    {
        cout << "\t" << name [i] << "\t\t" << gpa [i];
        cout << endl;
    }

    for (i=0 ; i<5 ; i++)
    {
        for (int j=0 ; j<1 ; j++)
        {
            cout << (int) name [i][j] << endl;



        }
    }

    cout << "\n\n******* Sorted data (w.r.t name) *******\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    for (i=0 ; i<5 ; i++)
    {
        cout << "\t" << name [i] << "\t\t" << gpa [i];
        cout << endl;
    }

    cout << endl;

    return 0;
}

Remember, only name should be sorted alphabetically. I have taken the ASCII values of the first characters of entered names in the middle forloop but:- 1- ASCII code for 's' is not the same as 'S' (That's a problem for me) 2- I can't seem create a logic to compare the ASCII values of the first letters of names then sort them accordingly. Then afterwards linking the name with the sorted letter list and displaying the result. Also the GPA should be linked with the names.

请记住,只有名称应按字母顺序排序。我在中间for循环中采用了输入名称的第一个字符的 ASCII 值,但是:- 1- 's' 的 ASCII 代码与 'S' 不同(这对我来说是个问题)2- 我似乎无法创建一个逻辑来比较名称的第一个字母的 ASCII 值,然后相应地对它们进行排序。然后将名称与排序后的字母列表联系起来并显示结果。此外,GPA 应与姓名相关联。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by splrs

If you convert the names' character to upper-case using std::toupperyou should then just be able to compare the the strings using < operator.

如果您使用将名称的字符转换为大写,std::toupper那么您应该能够使用 < 运算符比较字符串。

Edit: if you don't want to use std::sort:-)

编辑:如果你不想使用std::sort:-)

回答by denarced

Here's an answer using std::sort. I changed some of your C-like approach and using std::sort actually forces me to do it. The comparison function(compareStudents) needs objects so I had to create the struct. Vector has been used for the same reason although it would have been possible to keep using arrays but that's generally frowned upon.

这是使用 std::sort 的答案。我改变了你的一些类似 C 的方法,使用 std::sort 实际上迫使我这样做。比较函数(compareStudents)需要对象,所以我必须创建结构。Vector 也出于同样的原因使用,尽管可以继续使用数组,但这通常是不受欢迎的。

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

struct Student {
    string name;
    float gpa;

    Student(string name, float gpa) {
        this->name = name;
        this->gpa = gpa;
    }
};

bool compareStudents(Student a, Student b) {
    return a.name.compare(b.name) < 0;
}

int main() {
    const int studentCount = 2;
    vector<Student> studentVector;
    int i;

    for (i = 0 ; i < studentCount ; i++) {
        cout << "Enter name " << i + 1 << "  :   ";
        string name;
        cin >> name;
        cout << "Enter GPA     :   ";
        float gpa;
        cin >> gpa;
        cout << endl;

        studentVector.push_back(Student(name, gpa));
    }

    cout << "\n********** Your entered data **********\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    vector<Student>::iterator it = studentVector.begin();
    for (; it != studentVector.end(); ++it) {
        Student student = *it;
        cout << "\t" << student.name << "\t\t" << student.gpa;
        cout << endl;
    }

    sort(studentVector.begin(), studentVector.end(), compareStudents);

    cout << "\n\n******* Sorted data (w.r.t name) *******\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    it = studentVector.begin();
    for (; it != studentVector.end(); ++it) {
        Student student = *it;
        cout << "\t" << student.name << "\t\t" << student.gpa;
        cout << endl;
    }

    cout << endl;

    return 0;
}

回答by siddharth

    #include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
class stu
{
char name[40];
int cgpa;
public:
void assign()
{cin>>name;
cin>>cgpa;
}
void display()
{cout<<name<<endl<<cgpa<<endl;
}
friend void align(stu *s,int v);
};
void align(stu *s,int v)
{for(int j=0;j<v-1;j++)
{for(int i=0;i<v-j-1;i++)
{//buble sort
if(strcmp((s+i)->name,(s+i+1)->name)>0)
{char l[40];
strcpy(l,(s+i)->name);
strcpy((s+i)->name,(s+i+1)->name);
strcpy((s+i+1)->name,l);
//swapping cgpa
int t=(s+i)->cgpa;
(s+i)->cgpa=(s+i+1)->cgpa;
(s+i+1)->cgpa=t;
}
}}}

int main()
{stu s[10];int i;
for(i=0;i<5;i++)
s[i].assign();
align(s,5);
cout<<endl<<endl;
for(i=0;i<5;i++)
s[i].display();
}