初始化指针向量(自动) [英] initialize vector of pointers (automatically)
问题描述
当尝试通过std::vector
构造函数初始化指向NULL
的指针向量时,遇到编译错误.我简化了说明以使其简单:
I encountered a compilation error when trying to initialize a vector of pointers to NULL
by way of the std::vector
constructor. I simplify the instruction to keep it simple:
vector<int*> v (100,NULL)
我想这与const T& value= T()
(构造函数的参数)和非常大的NULL值之间的不兼容有关,但是我希望得到进一步的解释.
I guess it has something to do with an incompatibility between const T& value= T()
(the parameter of the constructor) and the very value NULL, but I would appreciate some further explanation.
谢谢
推荐答案
如果有相关的C ++ 11支持,则可以使用nullptr
:
If you have the relevant C++11 support, you could use nullptr
:
std::vector<int*> v(100, nullptr);
但是,在您的特定情况下,无需指定默认值,因此满足以下条件:
However, in your particular case, there is no need to specify a default value, so the following would suffice:
std::vector<int*> v(100);
这篇关于初始化指针向量(自动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!