如何初始化一个stl向量的对象,他们自己有非平凡的构造函数? [英] How do I initialize a stl vector of objects who themselves have non-trivial constructors?

查看:152
本文介绍了如何初始化一个stl向量的对象,他们自己有非平凡的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有下面的类:

class MyInteger {
private:
  int n_;
public:
  MyInteger(int n) : n_(n) {};
  // MORE STUFF
};



< 。我必须总是提供 int 来初始化它由于某种原因。然后假设我的代码中的某处需要一个向量< MyInteger> 。如何初始化向量<>

And suppose this class don't have a default trivial constructor MyInteger(). I must always supply an int to initialize it for some reason. And then suppose that somewhere in my code I need a vector<MyInteger>. How do I initialize each MyInteger component in this vector<>?

我有两种情况(可能的解决方案是一样的,但我会说它们),一个函数内的正常变量:

I have two situations (probably the solution is the same, but I'll state them anyway), a normal variable inside a function:

int main(){
    vector<MyInteger> foo(10);  //how do I initialize each 
                                //MyInteger field of this vector? 
    doStuff(foo);
}

并作为类中的数据:

class MyFunClass {
private:
   vector<MyInteger> myVector;

public:
   MyFunClass(int size, int myIntegerValue) : myVector(size) {}; 
   // what do I put here if I need the 
   // initialization to call MyInteger(myIntegerValue) for all 
   // components of myVector?
};

可以在初始化列表中执行,或者必须手动在MyFunClass(int,int)constructor?

Is it possible to do it just in the initialization list or must I write the initialization by hand in the MyFunClass(int, int) constructor?

这看起来非常基本,但我不知何故错过了它inmy的书,在网上找不到。

This seems so very basic, and yet I somehow missed it inmy book and can't find in the web.

推荐答案

有很多方法可以到达。下面是其中一些(没有特定的顺序)。

There are many ways to get there. Here are some of them (in no particular order of presence).

使用向量(size_type n,const T& t)构造函数。它用 t 的副本初始化向量。例如:

Use vector(size_type n, const T& t) constructor. It initializes vector with n copies of t. For example:

#include <vector>

struct MyInt
{
    int value;
    MyInt (int value) : value (value) {}
};

struct MyStuff
{
    std::vector<MyInt> values;

    MyStuff () : values (10, MyInt (20))
    {
    }
};

将元素逐个推入向量。当值应该不同时,这可能很有用。例如:

Push elements into vector one by one. This might be useful when values should be different. For example:

#include <vector>

struct MyInt
{
    int value;
    MyInt (int value) : value (value) {}
};

struct MyStuff
{
    std::vector<MyInt> values;

    MyStuff () : values ()
    {
        values.reserve (10); // Reserve memory not to allocate it 10 times...
        for (int i = 0; i < 10; ++i)
        {
            values.push_back (MyInt (i));
        }
    }
};

另一个选项是构造函数初始化列表,如果C ++ 0x是一个选项:

Another option is constructor initialization list, if C++0x is an option:

#include <vector>

struct MyInt
{
    int value;
    MyInt (int value) : value (value) {}
};

struct MyStuff
{
    std::vector<MyInt> values;

    MyStuff () : values ({ MyInt (1), MyInt (2), MyInt (3) /* ... */})
    {
    }
};

当然,有一个选项提供默认构造函数和/或使用 std :: vector

Of course, there is an option to provide default constructor and/or use something other than std::vector.

希望它有帮助。

这篇关于如何初始化一个stl向量的对象,他们自己有非平凡的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆