简体   繁体   English

在结构中初始化结构的向量

[英]Initializing a vector of a struct in a struct

I know how to get a struct in a struct working but what I can't get to work is vector of a struct in a struct. 我知道如何在结构中使用结构但是我无法工作的是结构中结构的向量。

Creating a vector of a struct on a normal basis works for example with: 在正常的基础上创建结构的向量例如:

vector<struct> str1(100);

but how do I do that if I have the following code: 但如果我有以下代码,我该怎么做呢:

struct attribures {
    string name;
    bool value;
};

struct thing {
    string name;
    double y;
    int x;
    vector<attributes> attrib;
};

How can I now initialize elements of the vector? 我现在如何初始化向量的元素? One thing I could do is something like the following: 我能做的一件事就是如下:

attributes a;
objec.attrib.push_back(a); // object is a struct of type thing

But that solution doesnt seem that elegant to me. 但是这个解决方案对我来说似乎并不优雅。 Is there anyway that is more of the first kind? 反正有没有第一种?

EDIT: sorry for the confusion. 编辑:抱歉混淆。 The "100" was actually just an example and in the second example it was actually also just an example, which is supposed to show how it could be done but doesn't seem very elegant to me. “100”实际上只是一个例子,在第二个例子中,它实际上也只是一个例子,它应该表明它是如何完成的,但对我来说似乎并不优雅。

Perhaps add a constructor to attributes : 也许为attributes添加一个构造函数:

struct attributes{
    attributes(const string& name, bool value) : name(name), value(value) {}
    string name;
    bool value;
};

and then: 接着:

 object.attrib.push_back(attributes("foo", true));

Your question is not clear, so I'm trying to guess what you want to do. 你的问题不明确,所以我想猜猜你想做什么。 If you want that thing is always initialized with 100 elements, you need to use a constructor (I also initialized x and y as they are undefined by default, so it's good to initialize them): 如果你想要总是用100个元素初始化那个东西,你需要使用一个构造函数(我也初始化了x和y,因为默认情况下它们是未定义的,所以最好初始化它们):

struct thing {
    string name;
    double y;
    int x;
    vector<attributes> attrib;
    thing() : y(0), x(0), attrib(100) {}

};

If you want to build a vector of 100 elements with a default value: 如果要使用默认值构建100个元素的向量:

attributes a;
a.name = "fOO";
std::vector<attributes> attrib(100, a);

This will give you a vector of 100 elements having "foo" as name. 这将为您提供包含“foo”作为名称的100个元素的向量。

And of course you can combine both examples ;) 当然,你可以结合两个例子;)

In case of C++0x, you have several possibilities: 在C ++ 0x的情况下,您有几种可能性:

#include <vector>
#include <string>    

int main () {
    struct Person {
        Person(std::string const &name, int age) : name (name), age(age) {}
        std::string name;
        int age;
    };

    std::vector<Person> vec { {"John", 24},
                              {"Dani", 32} };

    vec.emplace_back ("Frobster", -2);
    vec.push_back ({"Little unknown rascal", 7});
}

If you don't want to write a non-default constructor, you can still do: 如果您不想编写非默认构造函数,您仍然可以执行以下操作:

#include <vector>
#include <string>    

int main () {
    struct Person {
        std::string name;
        int age;
    };

    std::vector<Person> vec { {"John", 24},
                              {"Dani", 32} };

    vec.emplace_back (Person{"Frobster", -2});
    vec.push_back (Person{"Little unknown rascal", 7});
}

Though the emplace_back is superfluous then. 虽然emplace_back是多余的。

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

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