如何使用for循环从数组中打印出交替的信息

How do I use a for loop to print out alternating information from arrays?

本文关键字:信息 打印 for 何使用 循环 数组      更新时间:2023-10-16

我真的很抱歉让人困惑的标题,但我不知道如何表述这个问题,而不给你看代码。这就是我的问题。我试图打印出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个元素,然后是第二个3个,然后是第三个3个,以此类推。所以它看起来像(Id号,姓,年龄),重复20次,而不是(Id号x20,姓x20,年龄x20)。我该如何重构它,使它看起来像我想要的那样?

您需要将您的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;
};

您可以在以后学习getter和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";
}

注意你的数据是如何保存在一个逻辑位置的,如果你不想,你不需要硬编码一些学生,一个好的调试器现在会执行更多的边界检查。

生活的例子:http://ideone.com/gsvbYa

如果你不需要或不想使用直接的原始数组,你可以将所有这些信息包含在一个类结构中,你可以在这个类对象上实现一个print函数,或者添加一个重载的std::cout <<流操作符函数。

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;
};

这将是类声明,从这里你必须适当地实现函数。

  • 你可以简单地打印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;
      }