【问题标题】:Create an array that holds struct objects C++创建一个包含结构对象 C++ 的数组
【发布时间】:2015-08-17 02:19:29
【问题描述】:

我是一名新手程序员,我正在创建一个包含多个结构类型对象的程序。该程序需要接受用户输入,但我不知道该怎么做。首先,这是我用来定义结构的代码:

struct Apartment{
        int number;
        string owner;
        string condition;
}ap;

这是我用来询问用户输入的代码:

cout << "Enter the apartment number: " << endl;
cin >> ap.number;
cout << "Enter the name of the owner: " << endl;
cin >> ap.owner;
cout << "Enter the condition: " << endl;
cin >> ap.condition;

apartment building[50] = { ap.number, ap.owner, ap.condition};

最后一行代码是我如何尝试将对象保存在数组中,但我不知道它是否有效。 后来我需要打印所有的对象,所以如果你也帮我做这件事会很好。我使用 Visual Studio 2013 作为编译器,以防万一。

【问题讨论】:

  • 你认为最后一行在做什么?你所拥有的是“一系列公寓 = 一个公寓”。
  • 对你来说最简单的事情就是每次读入一个空的std::vector&lt;Apartment&gt;push_backApartment
  • paddy,你能解释一下怎么做吗?我是 C++ 的新手,我对 Python 有一些经验,所以我不太确定事情是如何工作的。

标签: c++ arrays struct


【解决方案1】:

首先,让我们了解你在做什么。

     struct Apartment{
            int number;
            string owner;
            string condition;
    }ap;

首先,您创建一个名为“ap”的Apartement 结构。

cout << "Enter the apartment number: " << endl;
cin >> ap.number;
cout << "Enter the name of the owner: " << endl;
cin >> ap.owner;
cout << "Enter the condition: " << endl;
cin >> ap.condition;

其次,(我假设您在 main 函数中)您要求用户输入一些信息。 您必须记住 cin 仅表示第一个单词并在它看到的第一个空格处停止如果您打算保存更多单词,则应该使用 getline。有关更多信息,请参阅此链接:http://www.cplusplus.com/doc/tutorial/basic_io/

apartment building[50] = { ap.number, ap.owner, ap.condition};

最后,你的最后一行会因为两个原因而崩溃。首先,因为类型公寓不存在。我相信你的意思是写公寓。其次,因为您无法创建这样的数组。我建议你看看这个:http://www.cplusplus.com/doc/tutorial/arrays/

我不确定你到底想做什么,所以我会给你一个代码示例,询问用户他有多少公寓,并询问他拥有多少公寓的信息。

struct Apartment{
            int number;
            string owner;
            string condition;
    };

int main() {
    cout << "Hello, how many apartment do you own?";
    int nbAppt = 0;
    cin >> nbAppt;
    Apartment appt[nbAppt];
    for(int i = 0; i < nbAppt; i++) {
        cout << "Appartment number " << i << endl;
        cout << "Enter the apartment number: ";
        cin >> appt[i].number;
        cout << "Enter the name of the owner: ";
        cin >> appt[i].owner;
        cout << "Enter the condition: " << endl;
        cin >> appt[i].condition;
    }
}

Ps:请注意,我假设您包含命名空间 std; Ps2:我没有包含任何相关的包含。

【讨论】:

    【解决方案2】:
    struct Apartment{
        int number;
        string owner;
        string condition;
    }ap;
    
    void AddApartment(vector<Apartment> &vtnew)
    {
        Apartment building;
        cout << "Enter the apartment number: " << endl;
        cin >> building.number;
        cout << "Enter the name of the owner: " << endl;
        cin >> building.owner;
        cout << "Enter the condition: " << endl;
        cin >> building.condition;
        vtnew.push_back(building);
    }
    
    void DisplayApt(vector<Apartment> vtnew)
    {
        vector<Apartment> ::iterator it;
        for (it = vtnew.begin(); it != vtnew.end(); it++)
        {
            cout << "apartment number" <<it->number;
            cout << "name of the owner" << it->owner;
            cout << "condition" << it->condition;
        }
    }
    
    int main()
    {
        vector<Apartment> vt;
        AddApartment(vt);
        AddApartment(vt);
        AddApartment(vt);
        DisplayApt(vt);
        return 0;
    }
    

    您在代码中所做的是为公寓声明一个数组以容纳 50 个公寓,但是当您使用向量时,我们无需事先给出计数,我们可以继续添加公寓,而不必担心大小!

    【讨论】:

    • 好的,现在我有一个问题。它连续多次请求对象,我需要它请求一个然后返回上一个函数,有没有办法做到这一点?
    • 没听懂,什么对象,什么意思?
    • 好的,这个程序就像一个公寓的注册表,所以它有一个菜单功能。其中一项功能是添加公寓,如果您选择它,用户必须输入对象的信息(编号、所有者、条件)。但它一次只问一个。然后返回菜单。不知道这次有没有说清楚。
    • 我建议你在这种情况下使用向量。
    【解决方案3】:

    我认为你在这里有一个困惑。

    Apartment building[50]
    

    表示这是一个由 50 个元素组成的数组,每个元素都是一个 Apartment 类型的结构体。

    例如应该是这样的:

    struct Apartment obj1;
    struct Apartment obj2;
    struct Apartment obj3;
    
    Apartment building[3] = {obj1, obj2, obj3};
    

    我想知道“将对象保存在数组中”的主要目的是什么?

    数组的每个元素都应该有相同的数据类型,而结构的每个字段可能没有相同的数据类型,所以你不应该“将对象保存在数组中”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-05
      • 2012-07-30
      • 2021-09-22
      • 2019-06-26
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2020-07-02
      相关资源
      最近更新 更多