简体   繁体   English

C ++:为什么boost :: ptr_vector resize需要对象具有默认构造函数

[英]C++: why boost::ptr_vector resize needs object to have default constructor

I am using a boost::ptr_vector over just std::vector as it will handle the deletion of all of the pointers for me. 我在std :: vector上使用boost :: ptr_vector,因为它将为我处理所有指针的删除。 However when I do: 但是,当我这样做时:

ptr_vector<SoftLabelRandomTreeFunctor> functors;
functors.resize(number_of_functors);

It complains that SoftLabelRandomTreeFunctor does not have a default constructor. 它抱怨SoftLabelRandomTreeFunctor没有默认构造函数。 However, I was under the impression that it would just need to resize big enough to fit number_of_functors * the size of a pointer to a SoftLabelRandomTreeFunctor , not number_of_functors * the size of a SoftLabelRandomTreeFunctor itself? 但是,我的印象是它只需要调整大小以适应number_of_functors *指向SoftLabelRandomTreeFunctor的指针的大小,而不是number_of_functors * SoftLabelRandomTreeFunctor本身的大小?

I am not really experienced with Boost, so take my answer with a grain of salt. 我对Boost并不是很有经验,所以请耐心等待我的回答。 However, skimming through the docs for boost::ptr_vector made me think that what you want (as follows from the comments to the question) should be possible to do this way: 但是,浏览boost::ptr_vector的文档让我觉得你想要的东西(从问题的评论中如下)应该可以这样做:

boost::ptr_vector< boost::nullable<SoftLabelRandomTreeFunctor> > functors;
functors.resize(number_of_functors, 0);

The references for you to read and make your own conclusion: 参考资料供您阅读并做出自己的结论:

When you write functors.resize(number_of_functors) you potentially increase the size of the vector to contain number_of_functors elements inside. 当你编写functors.resize(number_of_functors)你可能会增加向量的大小以包含其中的number_of_functors元素。 Since ptr_vector by default disallows storing NULL values, it needs to put a meaningfull data into the inflated array. 由于ptr_vector默认情况下不允许存储NULL值,因此需要将有意义的数据放入膨胀的数组中。 The function intends to call new SoftLabelRandomTreeFunctor() for every new element and it requires a default constructor for that. 该函数打算为每个新元素调用new SoftLabelRandomTreeFunctor() ,并且它需要一个默认的构造函数。

If you want to allow NULLs, you need the boost::nullable as suggested by Alexey Kukanov answer, and as in the tutorial included in the manual ( (here) ). 如果你想允许NULL,你需要像Alexey Kukanov回答所建议的boost::nullable ,以及手册中包含的教程( (这里) )。

However, if you just intend to reserve enough memory for number_of_functors elements without semantically creating them and without increasing the array size --- you don't need nullable and you should call instead: 但是,如果您只是打算为number_of_functors元素保留足够的内存而不会在语义上创建它们而不增加数组大小 - 您不需要为nullable ,您应该调用:

functors.reserve(number_of_functors)

Note that after this, you still need to increase the array size when you put new elements (eg via push_back ). 请注意,在此之后,您仍需要在放置新元素时增加数组大小(例如,通过push_back )。 You will have a guarantee though that push_back won't call a memory reallocation as long as your size doesn't exceed number_of_functors . 只要您的大小不超过number_of_functors您就可以获得保证,即push_back不会调用内存重新分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM