在学生班级之间排序名称
问题描述:
编辑。我想感谢在我的代码中帮助过我的两个人。截至PST 9/25/2014下午7:22,我修复了我的代码。尽管如此,排序仍然有问题。在学生班级之间排序名称
所以我想教自己如何使用C++为了在学校里领先我的编程课。我已经采用了Java,所以我对编码的结构比较熟悉,但不完全确定要输入什么内容。这个练习问题是:
1)询问用户有多少人想进入列表。
2)阅读3个字符串,然后将该信息转换为学生课,并将学生放入一个向量中。
3)按名称对列表进行排序。 (额外学分将按学号排序)
4)列出每个学生的信息。
5)询问用户,而答案是“是”,在列表中找到的名称和打印信息相关的名称
而在Java中,这似乎是很容易的,我有一个非常困难时期了解C++中发生了什么。
我目前只有1,2,4和5完成。这是我迄今为止所拥有的。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
class Student
{
string myName, myStudentID, myClassID;
public:
Student(string name, string studentID, string classID)
{
myName = name;
myStudentID = studentID;
myClassID = classID;
}
string getName() { return myName; }
string getStudentID() { return myStudentID; }
void printInfo() { cout << myName << setw(15) << myStudentID << setw(10) << myClassID << endl; }
};
int main()
{
int num;
std::vector<Student> studentList;
cout << "How many students do you wish to add to the student list ? " << endl;
cin >> num;
cin.ignore();
for (int a = 0; a < num; a++)
{
string inputName, inputStudentID, inputClassID;
//get name
cout << "Please enter the Student name : ";
getline(cin, inputName);
//get student ID
cout << "Please enter the Student ID number : ";
getline(cin, inputStudentID);
//get class ID
cout << "Please enter the Student class ID : ";
getline(cin, inputClassID);
Student thisStudent(inputName, inputStudentID, inputClassID);
studentList.push_back(thisStudent);
cout << endl;
}
//sort(studentList.begin(), studentList.end());
/*
I never figured out how to sort the list by name.
I do not know how to compare the name of studentList[0] and studentList[1]
to put them in alphabetical order.
*/
cout << endl;; // FORMATTING
cout << "The student list has a size of " << studentList.size() << endl;
cout << endl;; // FORMATTING
cout << "Student Name" << setw(15) << "Student ID" << setw(10) << "Class ID" << endl;
for (int a = 0; a < studentList.size(); a++)
{
studentList[a].printInfo();
}
cout << endl;; // FORMATTING
string searchedName;
char answer;
do
{
cout << endl;; // FORMATTING
cout << "Please type the name of the person you want to search for: ";
getline(cin, searchedName);
for (int a = 0; a < studentList.size(); a++)
{
if (searchedName.compare(studentList[a].getName()) == 0)
{
studentList[a].printInfo();
break;
}
else
if (a == (studentList.size() - 1)) //If went to end of the list, tell them name not found.
cout << "There is no " << searchedName << " in the list. \n";
}
cout << "Would you like to search for another person? \n";
cout << "Y for Yes, N for No. \n";
cin >> answer;
cin.ignore();
} while (answer == 'Y' || answer == 'y');
return 0;
}
答
您可以创建一个静态类学生比较函数的std ::排序
class Student
{
string myName, myStudentID, myClassID;
// ...
static bool compare_name(const Student & lStudent, const Student & rStudent)
{return (lStudent.myName < rStudent.myName);}
};
// ...
std::sort(studentList.begin(), studentList.end(), Student::compare_name);
,或者如果你的编译器支持lambda函数的使用方法:
std::sort(studentList.begin(), studentList.end(),
[studentList](const Student & lStudent, const Student & rStudent)
{return (lStudent.myName < rStudent.myName);});
如果你的程序“之类的“你在Visual Studio中,并且你正在寻求领先于你的编程类,那么现在是学习如何调试的好时机(F5应该在VS2013中开始调试) – Foggzie 2014-09-26 01:05:39
移动'cin。忽视()'如果for循环开始。阅读[这个答案]的第一部分(http://stackoverflow.com/a/21567292/701092)了解发生了什么 – 0x499602D2 2014-09-26 01:34:34