stl向量和c ++:如何.resize没有默认的构造函数? [英] stl vector and c++: how to .resize without a default constructor?
本文介绍了stl向量和c ++:如何.resize没有默认的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能告诉STL,具体为方法resize()在向量中,使用非默认的构造函数初始化对象,并使用哪些参数?
how could I tell STL, specifically for the method resize() in vector, to initialize objects with a constructor other than default, and with which parameters?
I平均值:
class something {
int a;
something (int value);
}
std::vector<something> many_things;
many_things.resize (20);
<参数到那个构造函数?
more generally, how could I force STL to use my costructor when it needs to create objects, and pass parameters to that constructor?
在我的例子中添加一个默认构造函数不是一个选项,我不想使用指针数组来解决问题。 / p>
in my case adding a default constructor is not an option, and I'd prefer not to use an array of pointers to solve the problem.
推荐答案
使用2参数重载:
many_things.resize(20,something ));
这篇关于stl向量和c ++:如何.resize没有默认的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文