How do I initialize a stl vector of objects who themselves have non-trivial constructors?(如何初始化本身具有非平凡构造函数的对象的 stl 向量?)
问题描述
假设我有以下课程:
class MyInteger {
private:
int n_;
public:
MyInteger(int n) : n_(n) {};
// MORE STUFF
};
假设这个类没有默认的普通构造函数MyInteger()
.由于某种原因,我必须始终提供一个 int
来初始化它.然后假设在我的代码中的某个地方我需要一个 vector
.如何初始化此 vector<>
中的每个 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) 构造函数中手动编写初始化?
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?
这看起来非常基本,但我不知何故在我的书中错过了它并且在网上找不到.
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).
使用 vector(size_type n, const T& t)
构造函数.它用 n
个 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) /* ... *
本文标题为:如何初始化本身具有非平凡构造函数的对象的 stl 向量?


基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01