簡體   English   中英

C ++用另一個向量初始化向量

[英]C++ Initialize vector with another vector

考慮FooBar類:

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

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

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

如何寫構造Bar(const std::vector<int> & v)使得每個元件v可初始化Foo對象,具有Foo(int)

您可以使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM