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

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

C++ 按字母顺序排列名称,c++,arrays,sorting,ascii,alphabetical,C++,Arrays,Sorting,Ascii,Alphabetical,我正试着按字母顺序给名字排序 e、 g如果用户输入姓名和GPA: Names GPA Peter 2.8 Robert 5.6 David 7.8 输出应为:- Names GPA David 7.8 Peter 2.8 Robert 5.6 这是我到目前为止的程序(不完整的):- #包括 使用名称空间std; int main() { 字符名[5][2

我正试着按字母顺序给名字排序 e、 g如果用户输入姓名和GPA:

Names          GPA
Peter          2.8
Robert         5.6
David          7.8
输出应为:-

Names          GPA
David          7.8
Peter          2.8
Robert         5.6
这是我到目前为止的程序(不完整的):-

#包括
使用名称空间std;
int main()
{
字符名[5][25];
浮动gpa[5];
int i;

对于(i=0;i如果您使用
std::toupper
将名称的字符转换为大写,那么您应该能够使用
运算符比较字符串


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

这里有一个使用std::sort的答案。我改变了一些类似C的方法,使用std::sort实际上迫使我这么做。比较函数(compareStudents)因为需要对象,所以我不得不创建struct.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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构学生{
字符串名;
浮动gpa;
学生(字符串名称,浮点gpa){
此->名称=名称;
这->gpa=gpa;
}
};
布尔比较学生(学生a、学生b){
返回a.name.compare(b.name)<0;
}
int main(){
const int studentCount=2;
向量学生向量;
int i;
对于(i=0;i>名称;
cin>>cgpa;
}
无效显示()
{coutcgpa;
(s+i+1)->cgpa=t;
}
}}}
int main()
{stu s[10];国际一级;

for(i=0;iYes'sort'对我来说很复杂。但是你能提供我如何使用它的参考吗?要比较字符串,你可以使用“string1toupper
,我可以在内部将小写输入转换为大写?然后我应该比较字母的ASCII值?或者我可以只比较名称的第一个字符吗?我的意思是,你可以通过
name[0]
。创建两个临时变量(
char n1[5];char n2[5]
)并从原始数组中逐个复制字符,例如
nm1[j]=name[i][j]
,然后
n1
将比较它们。很抱歉,请您编辑
向量
排序
部分,并将其替换为使用循环等的逻辑算法?我还没有研究过这些,这对我来说很难理解。@denarced。做得好。您写了…….学生=*it;……您是指nst Student&Student=*it;…?不,但这可能是正确的:)我写代码时对细节的关注最少,因为只有基本思想才是重要的。最近我一直没有处理C++。@ HamzaUmar,如果你要求我重写STD::排序,然后不,我不会这样做。实际上,我从来没有编写过任何一种合适的排序算法,因为Quasy排序(例如)已经存在,程序员不必知道它的复杂细节。我很乐意回答您的任何问题,以澄清当前的代码。
#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;
}
    #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();
}