如何在构造函数中使用 <vector>

塞比兹

我如何<vector>用于需要通过构造函数赋值的对象数组例如,具有名称、年龄的类将需要通过构造函数提供的信息进入数组Student(string n, int a ) { name = n , age = a }

所有数据将通过键盘给出..

晚安

这是一个能够使用向量获取和存储学生列表的姓名和年龄的程序示例代码。之后,它打印存储的信息。我使用 MSVC 作为编译器,所以如果你不在 Windows 上,你可以删除system("pause")

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

using namespace std;

class Student {
public:
    Student(string n, int a) : name(n), age(a) {}

    string GetName(void) { return name; }
    int GetAge(void) { return age; }

private:
    string name;
    int age;

};

int main(void) {
    vector<Student> students;

    unsigned int n;

    cout << "How many students are there?" << endl;
    cin >> n;

    for (unsigned int i = 0; i < n; ++i) {
        string name;
        int age;

        cout << endl << "Please give me the information of the student " << i + 1 << endl;

        cout << "What is the name of the student?" << endl;
        cin >> name;

        cout << "What is the age of the student?" << endl;
        cin >> age;

        students.push_back(Student(name, age));
    }

    cout << endl << "Printing information of the students" << endl << endl;

    for (unsigned int i = 0; i < n; ++i) {
        Student& student = students[i];

        cout << "Student " << i + 1 << " is " << student.GetName() << " and is " << student.GetAge() << " years old." << endl;
    }

    system("pause");

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在std :: vector构造函数中使用'{}'作为结束迭代器

来自分类Dev

在类构造函数中使用vector时发生运行时错误?

来自分类Dev

如何在构造函数中使用@Value?

来自分类Dev

如何在构造函数中使用setTimeout

来自分类Dev

gsl_vector的构造函数

来自分类Dev

在元函数定义中使用mpl :: vector

来自分类Dev

使用std :: vector时如何将索引信息传递给元素构造函数?

来自分类Dev

如何在函数构造函数中使用标签

来自分类Dev

编译器如何区分std :: vector的构造函数?

来自分类Dev

尝试在类中使用 vector<vector<int>>

来自分类Dev

如何在构造时为std :: vector保留内存?

来自分类Dev

如何使包装器类将其构造函数参数转发到std :: vector的构造函数?

来自分类Dev

在类构造函数中设置std :: vector,为元素构造函数使用不同的值

来自分类Dev

如何在构造函数中使用参数自动接线?

来自分类Dev

如何在构造函数中使用注入的对象?

来自分类Dev

如何在构造函数中使用AutoMapper映射到“ this”

来自分类Dev

如何在getline中使用stringstream构造函数?

来自分类Dev

如何在Javascript中使用父子构造函数?

来自分类Dev

如何在构造函数中使用unique_ptr?

来自分类Dev

如何在类构造函数参数中使用IEnumerable <T>

来自分类Dev

如何在React中使用图像构造函数?

来自分类Dev

如何在C ++中的类构造函数中使用线程?

来自分类Dev

如何在Angular中使用两个构造函数

来自分类Dev

如何在C#中使用构造函数调用方法?

来自分类Dev

如何在Javascript中使用父子构造函数?

来自分类Dev

Python:如何在Django Form类中使用构造函数?

来自分类Dev

如何在主程序中使用构造函数注入

来自分类Dev

如何在 Scala 构造函数中使用“静态最终常量”?

来自分类Dev

如何使用带有 Boost.Python 的 std::map 或 std::vector 参数的构造函数包装 C++ 类?

Related 相关文章

  1. 1

    在std :: vector构造函数中使用'{}'作为结束迭代器

  2. 2

    在类构造函数中使用vector时发生运行时错误?

  3. 3

    如何在构造函数中使用@Value?

  4. 4

    如何在构造函数中使用setTimeout

  5. 5

    gsl_vector的构造函数

  6. 6

    在元函数定义中使用mpl :: vector

  7. 7

    使用std :: vector时如何将索引信息传递给元素构造函数?

  8. 8

    如何在函数构造函数中使用标签

  9. 9

    编译器如何区分std :: vector的构造函数?

  10. 10

    尝试在类中使用 vector<vector<int>>

  11. 11

    如何在构造时为std :: vector保留内存?

  12. 12

    如何使包装器类将其构造函数参数转发到std :: vector的构造函数?

  13. 13

    在类构造函数中设置std :: vector,为元素构造函数使用不同的值

  14. 14

    如何在构造函数中使用参数自动接线?

  15. 15

    如何在构造函数中使用注入的对象?

  16. 16

    如何在构造函数中使用AutoMapper映射到“ this”

  17. 17

    如何在getline中使用stringstream构造函数?

  18. 18

    如何在Javascript中使用父子构造函数?

  19. 19

    如何在构造函数中使用unique_ptr?

  20. 20

    如何在类构造函数参数中使用IEnumerable <T>

  21. 21

    如何在React中使用图像构造函数?

  22. 22

    如何在C ++中的类构造函数中使用线程?

  23. 23

    如何在Angular中使用两个构造函数

  24. 24

    如何在C#中使用构造函数调用方法?

  25. 25

    如何在Javascript中使用父子构造函数?

  26. 26

    Python:如何在Django Form类中使用构造函数?

  27. 27

    如何在主程序中使用构造函数注入

  28. 28

    如何在 Scala 构造函数中使用“静态最终常量”?

  29. 29

    如何使用带有 Boost.Python 的 std::map 或 std::vector 参数的构造函数包装 C++ 类?

热门标签

归档