39

假设我有以下课程:

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

并且假设这个类没有默认的平凡构造函数MyInteger()int由于某种原因,我必须始终提供一个来初始化它。然后假设在我的代码中某处我需要一个vector<MyInteger>. 我如何初始化MyInteger这个中的每个组件vector<>

我有两种情况(可能解决方案是相同的,但我还是会说明它们),一个函数内部的普通变量:

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) 构造函数中手动编写初始化?

这似乎非常基本,但我不知何故在我的书中错过了它,在网上找不到。

4

5 回答 5

40

有很多方法可以到达那里。以下是其中一些(不分先后顺序)。

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

#include <vector>

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

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

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

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

#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 是一个选项:

#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.

希望能帮助到你。

于 2011-05-26T17:59:14.753 回答
12

如果向量的元素不是默认可构造的,那么有些事情你不能用向量做。您不能这样写(示例 1):

vector<MyInteger> foo(10);

但是,您可以这样写(示例 2):

vector<MyInteger> foo(10, MyInteger(37));

(这只需要一个复制构造函数。)第二个参数是向量元素的初始值设定项。

在你的情况下,你也可以写:

vector<MyInteger> foo(10, 37);

...因为 MyInteger 有一个以“int”作为参数的非显式构造函数。因此编译器会将 37 转换为 MyInteger(37) 并给出与示例 2 相同的结果。

您可能想研究有关 std::vector 的文档

于 2011-05-26T17:53:42.797 回答
6
vector<MyInteger> foo(10, MyInteger(MY_INT_VALUE));

MyFunClass(int size, int myIntegerValue) : myVector(size, MyInteger(myIntegerValue)) {}; 
于 2011-05-26T17:53:52.703 回答
6

除了很好地回答了这个问题的所有答案之外,如果你的类 MyInteger 不是可复制构造的,你可以使用这个技巧:vector< MyInteger>你可以创建而不是创建vector< shared_ptr< MyInteger > >

于 2011-05-26T18:19:28.047 回答
1

无需参考底层对象即可使用初始化列表。

#include <string>
#include <vector>
using namespace std;


class Test
{
   public:
   struct NumStr
   {
      int num;
      string str;
   };

   Test(vector<int> v1,vector<NumStr> v2) : _v1(v1),_v2(v2) {}
   vector<int> _v1;
   vector<NumStr> _v2;
};

int main()
{
   Test t={ {1,2,3}, {{1,"one"}, {2,"two"}, {3,"three"}} };
   cout << t._v1[1] << " " << t._v2[1].num << " " << t._v2[1].str << endl;
   return 0;
}

输出:2 2 2

于 2018-07-15T17:42:05.160 回答