简体   繁体   中英

C++ Initialize vector with another vector

Consider the class Foo and Bar :

class Foo
{
public:
    Foo() = default;
    Foo(int);
};

class Bar
{
public:
    Bar() = default;
    Bar(const std::vector<int> & v);

private:
    std::vector<Foo> data_;
};

How to write the constructor Bar(const std::vector<int> & v) so that each element of v can initialize a Foo object with Foo(int) ?

您可以使用:

Bar(const std::vector<int> & v) : data_(v.begin(), v.end()) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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