我对这个令人困惑的标题感到非常抱歉,但我不知道如何在不向您展示代码的情况下提出问题。所以这是我的问题。我正在尝试打印出20个学生信息,这些信息全部包含在3个不同的数组中(身份证号码,姓氏和年龄)。数组(和向量)是这样的:
vector<int> studentNumber (20);
int age [20] {20, 21, 22, 42, 55, 28, 20, 20, 19,19, 22, 23, 25, 26, 24, 23, 19, 22, 21, 20};
string lastName [20] {"Simmons", "Jones", "James", "Little", "Russell", "Haynes", "Marcotte", "Kemper", "Vandergore", "Hume", "Stephens", "Jensen", "Biersack", "Sykes", "Joseph", "Dunn", "Hai", "Meteos", "Aphromoo", "Faker"};
我使用3 for循环打印出所有这些信息,我让它们都工作得很好。他们是这样的:
void getAllStudentInfo() {
for (vector<int>::size_type i = 0; i <= 20; i++) {
cout << "Student's ID number is: " << 400 + i << endl;
}
for (int i = 0; i < 20; i++) {
cout << "Student's last name is: " << lastName[i] <<endl;
}
for (int i = 0; i < 20; i++) {
cout << age[i] << endl;
}
return;
}
所以他们现在所做的就是打印出20个身份证号码,20个姓氏,然后20个年龄段的信息。我最理想的是他们要做的是打印每个数组中的前3个元素,然后是后三个元素,然后是第三个元素,依此类推。所以它看起来像(身份证号码,姓氏,年龄),重复二十次而不是(身份证号码x20,姓氏x20,年龄x20)。我将如何重构它以使它看起来像我喜欢的那样?
答案 0 :(得分:0)
您需要将for循环组合成一个循环。
首先,您选择的数据结构是不寻常的。为什么要混合矢量和数组?对所有这三个使用向量可能更好。然后可以在运行时确定学生数量,加上好的调试器将执行边界检查。您还应该避免在代码中硬编码20等魔术数字。如果需要更改数字,则更新代码会变得更加困难。此外,我不确定studentNumber
向量的重点是什么,因为你从未在其中存储任何数据。你只想要一个柜台吗? (使用int)
使用您的数据结构的代码如下:
for (size_t i = 0; i < numStudents; i++)
{
cout << "Student's ID number is: " << 400 + i << "\n";
cout << "Student's last name is: " << lastName[i] << "\n";
cout << age[i] << "\n";
}
根据您希望如何设计程序,如果将这三个信息组合到一个对象中可能会更好。这是一个简单的方法:
struct Student
{
Student(int id, int age, const string& name)
: id(id), age(age), name(name)
{}
int id;
int age;
string name;
};
您可以在稍后了解它们时添加getet和setter等细节。
现在填写学生的数据结构,如下:
vector<Student> allStudents;
// optional - if you know the number of students in advance, you can give a hint to the vector to increase its performance. the number does not need to be exact and calling reserve does not increase the number of elements in the vector right away
allStudents.reserve(20);
// add student data, possibly from a file?
allStudents.push_back(Student(400, 15, "Billy"));
allStudents.push_back(Student(401, 16, "Sally"));
// alternative slightly more efficient syntax for C++11
allStudents.emplace_back(402, 15, "Jill");
现在只有一种数据结构供您迭代。你可以这样做:
for (size_t i = 0; i < allStudents.size(); i++)
{
cout << "Student's ID number is: " << allStudents[i].id << "\n";
cout << "Student's last name is: " << allStudents[i].name << "\n";
cout << allStudents[i].age << "\n";
}
在C ++ 11中,您可以使用更方便的语法:
// remove the const keyword if you want to be able to write to student objects in the vector
for (const auto& student : allStudents)
{
cout << "Student's ID number is: " << student.id << "\n";
cout << "Student's last name is: " << student.name << "\n";
cout << student.age << "\n";
}
请注意您的数据是如何在一个逻辑位置保存在一起的,如果您不想,您不需要对许多学生进行硬编码,而且一个好的调试器现在将执行更多的边界检查。
答案 1 :(得分:0)
如果您不需要或不想使用直接原始数组,则可以包含与类结构相关的所有信息,您可以在该类对象上实现打印函数或添加重载的std :: cout&lt ;&LT;流操作员功能。
struct StudentInformation {
unsigned m_id;
unsigned m_age;
std::string m_lastName;
StudentInformation( unsigned uId, unsigned uAge, const std::string& strLastName ) :
m_id( uId ), m_age( uAge ), m_lastName( strLastName ) {
};
class StudentInformationList {
friend std::ostream& operator<<( std::ostream& out, const StudentInformation* const studentInformation );
private:
std::vector<StudentInformation> m_vStudents;
public:
StudentInformation();
explicit StudentInformation( StudentInformation& studentInformation );
void addStudentToList( const StudentInformation& studentInformation );
std::vector<StudentInformation>& getStudentList() const;
StudentInformation& getStudent( const unsigned id ) const;
StudentInformation& getStudent( const std::string& strName ) const;
};
这将是类声明,从这里你必须适当地实现这些功能。
答案 2 :(得分:0)
您只需在单循环中打印ID,姓名和年龄。
void getAllStudentInfo()
{
vector<int> studentNumber(20);
int age [20] {20, 21, 22, 42, 55, 28, 20, 20, 19,19, 22, 23, 25, 26, 24, 23, 19, 22, 21, 20};
string lastName [20] {"Simmons", "Jones", "James", "Little", "Russell", "Haynes", "Marcotte", "Kemper", "Vandergore", "Hume", "Stephens", "Jensen", "Biersack", "Sykes", "Joseph", "Dunn", "Hai", "Meteos", "Aphromoo", "Faker"};
int i;
for (vector<int>::size_type i = 0; i < 20; i++)
{
cout << "Student's ID number is: " << 400 + i << endl;
cout << "Student's last name is: " << lastName[i] <<endl;
cout << age[i] << endl;
}
return;
}