简体   繁体   English

我可以在结构中放一个列表吗?

[英]Can I put a list in a struct?

I am trying to use a list as part of a struct due to the fact our products come in several colors. 我试图使用列表作为结构的一部分,因为我们的产品有多种颜色。 Is this some thing I can do or should i just use an array? 这是我可以做的一些事情,还是我应该使用数组? Either way i have not found any examples on the web. 无论哪种方式,我都没有在网上找到任何例子。

#include "stdafx.h"
#include<iostream>  
#include<string>  
#include<sstream>  
#include<cctype> 
#include<list>

using namespace std;

////////////////////////////////////////////////////////////////////////////////////////
//  Constances
////////////////////////////////////////////////////////////////////////////////////////

#define N_PRODUCT 3

struct Brand_t {
int Model_Num;
string Product_Name;
list<string> Colors;
} brands [N_COLOR];

////////////////////////////////////////////////////////////////////////////////////////
//  Functions
////////////////////////////////////////////////////////////////////////////////////////


int main()
{
string mystr;
int n;

for (n=0; n < N_PRODUCT; n++)
{
    cout << "Enter Model Number: ";
    std::getline (cin,mystr);
    stringstream(mystr) >> brands[n].model_Num,4;
    cout << "Enter Product Name: ";
    getline(cin,classrooms[n].Product_Name);
    list<string>::iterator it;
    Students.push_back ("Red");
    Students.push_back ("Yellow");
    Students.push_back ("Blue");
}

return 0;
}

Yes, this can be done. 是的,这可以做到。 Thanks to the RAII, the list object will be automatically initialized and freed based on the lifetime of your struct. 由于RAII, list对象将根据结构的生命周期自动初始化和释放。 Take note that even thought this is not the case in other programming languages such as Object Pascal, the struct and class are effectively the same thing in C++, the only difference being default visibility of member methods and variables, as other answers have noted. 请注意,即使在其他编程语言(如Object Pascal)中也不是这种情况, structclass在C ++中实际上是相同的,唯一的区别是成员方法和变量的默认可见性,正如其他答案所指出的那样。

You can not put non- PODS objects into unions though. 但是,您不能将非PODS对象放入联合。

I suggest you use a std::vector or a std::array instead of a C-style array. 我建议你使用std::vectorstd::array而不是C风格的数组。 A std::vector would probably be most useful if your array is supposed to be of dynamic size. 如果你的数组应该是动态大小的话, std::vector可能是最有用的。 If you use plain new or even malloc() , then your objects will NOT be automatically freed (not even initialized with malloc() ) 如果你使用普通的new或甚至malloc() ,你的对象将不会被自动释放(甚至没有用malloc()初始化)

Yes, there is no reason not to. 是的,没有理由不这样做。 The only differences between a struct and a class in C++ are C ++中structclass之间的唯一区别是

A) struct members are public by default, and A) struct成员默认是公共的,并且

B) struct inheritance is public by default (ie, struct A : B is equivalent to class A : public B where B is a class .) B) struct继承默认是公共的(即struct A : B等同于class A : public B ,其中Bclass 。)

Using a list, or any object, inside of a struct is perfectly acceptable as long as the object has or supports a default constructor. 只要对象具有或支持默认构造函数,在结构内使用列表或任何对象是完全可以接受的。

The default constructor is required because when a struct variable is first declared, it is also initialized. 默认构造函数是必需的,因为首次声明struct变量时,它也会被初始化。 Any object inside of it will also be initialized by calling the default constructor. 其中的任何对象也将通过调用默认构造函数进行初始化。

There's nothing wrong with having an instance of a class as a member of a struct in C++, UNLESS you're relying on C++ treating instance of the struct as POD (plain old data) objects. 在C ++中将类的实例作为结构的成员没有任何问题,除非您依赖于C ++将结构的实例视为POD(普通旧数据)对象。 (Like with bit-wise copying, etc. (与逐位复制等一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM