简体   繁体   English

在 c++ 中创建结构数组

[英]creating an array of structs in c++

I'm trying to create an array of structs.我正在尝试创建一个结构数组。 Is the code below valid?下面的代码有效吗? I keep getting an expected primary-expression before '{' token error. expected primary-expression before '{'

 int main() { int pause; struct Customer { int uid; string name; }; Customer customerRecords[2]; customerRecords[0] = {25, "Bob Jones"}; customerRecords[1] = {26, "Jim Smith"}; cin >> pause; return 0; }

Try this:尝试这个:

 Customer customerRecords[2] = {{25, "Bob Jones"}, {26, "Jim Smith"}};

You can't use an initialization-list for a struct after it's been initialized.初始化后,您不能将初始化列表用于struct You've already default-initialized the two Customer structs when you declared the array customerRecords .在声明数组customerRecords时,您已经默认初始化了两个Customer结构。 Therefore you're going to have either use member-access syntax to set the value of the non-static data members, initialize the structs using a list of initialization lists when you declare the array itself, or you can create a constructor for your struct and use the default operator= member function to initialize the array members.因此,您将要么使用成员访问语法来设置非静态数据成员的值,要么在声明数组本身时使用初始化列表列表初始化结构,或者您可以为您的结构创建构造函数并使用默认的operator=成员 function 来初始化数组成员。

So either of the following could work:因此,以下任何一项都可以工作:

 Customer customerRecords[2]; customerRecords[0].uid = 25; customerRecords[0].name = "Bob Jones"; customerRecords[1].uid = 25; customerRecords[1].namem = "Jim Smith";

Or if you defined a constructor for your struct like:或者,如果您为结构定义了一个构造函数,例如:

 Customer::Customer(int id, string input_name): uid(id), name(input_name) {}

You could then do:然后你可以这样做:

 Customer customerRecords[2]; customerRecords[0] = Customer(25, "Bob Jones"); customerRecords[1] = Customer(26, "Jim Smith");

Or you could do the sequence of initialization lists that Tuomas used in his answer.或者您可以执行 Tuomas 在他的回答中使用的初始化列表序列。 The reason his initialization-list syntax works is because you're actually initializing the Customer structs at the time of the declaration of the array, rather than allowing the structs to be default-initialized which takes place whenever you declare an aggregate data-structure like an array.他的初始化列表语法起作用的原因是因为您实际上是在声明数组时初始化Customer结构,而不是允许结构被默认初始化,这在您声明聚合数据结构时发生数组。

Some compilers support compound literals as an extention, allowing this construct:一些编译器支持复合文字作为扩展,允许这种结构:

 Customer customerRecords[2]; customerRecords[0] = (Customer){25, "Bob Jones"}; customerRecords[1] = (Customer){26, "Jim Smith"};

But it's rather unportable.但它相当不便携。

It works perfectly.它完美地工作。 I have gcc compiler C++11 ready.我已经准备好 gcc 编译器 C++11。 Try this and you'll see:试试这个,你会看到:

 #include <iostream> using namespace std; int main() { int pause; struct Customer { int uid; string name; }; Customer customerRecords[2]; customerRecords[0] = {25, "Bob Jones"}; customerRecords[1] = {26, "Jim Smith"}; cout << customerRecords[0].uid << " " << customerRecords[0].name << endl; cout << customerRecords[1].uid << " " << customerRecords[1].name << endl; cin >> pause; return 0; }

you can use vector.你可以使用矢量。 First Define the Struct.首先定义结构。

 struct Customer { int uid; string name; };

Then,然后,

 vector<Customer> array_of_customers;

By using vector, you will have more freedom and access in the array of structure.通过使用向量,您将在结构数组中拥有更多的自由和访问权限。

Now if want to add an struct element in the define array.现在如果想在定义数组中添加一个结构元素。 You can use您可以使用

array_of_customer.push_pack(/* struct element here */)

Example:例子:

 Customer customer1; customer1.uid = 01; customer1.name = "John Doe"; Customer customer2; customer2.uid = 02; customer2.name = "Blah Blah"; array_of_customers.push_back(customer1); array_of_customers.push_back(customer2);

you have now an array of struct.你现在有一个结构数组。

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

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