简体   繁体   English

如何初始化stl容器中存在的类的数据?

[英]how to init data of class that exists in stl container?

For example, I have class A: 例如,我有A类:

 class A{
           int value_;
           public:
           A(A& a){
                value_ = a.value_;
           }
           A(int value){
                value_ = value;
           }
 };

I want a vector of class A but I'd like to pass a value to A(int value) for all of them. 我想要一个类A的向量,但我想将值传递给所有它们的A(int value)

 std::vector<A,allocator<A>> my_vector;
  • What is the best way to do it? 最好的方法是什么?
  • Is there a way by using allocator? 有没有办法使用分配器?

With the new standard added functionality was granted to objects of Allocator type. 使用新标准,添加的功能被授予Allocator类型的对象。

One of the features added was that Allocators now allows emplacement construction, aka. 添加的功能之一是Allocators现在允许进驻建筑,也就是说。 construction of objects using a constructor other than copy/move. 使用除复制/移动之外的构造函数构造对象。


 template< class U, class... Args > void construct( U* p, Args&&... args ); 

The standard does guarantee that STL containers must use this new feature, and with that said you could implement your own allocator just for the purpose of default initializing a non-default-initializable object. 该标准确实保证了STL容器必须使用这个新功能,并且说明你可以实现自己的分配器,只是为了默认初始化一个非默认初始化对象。


It's not the prettiest solution, but whatever floats your boat.. 这不是最漂亮的解决方案,但无论如何漂浮在你的船上......

The allocator has nothing to do with that part of object initialization, it's only purpose is to allocate/deallocate memory, the type of initialization you are referring to is done elsewhere. 分配器与对象初始化的那一部分无关,它的唯一目的是分配/释放内存,你所指的初始化类型是在别处完成的。

The only constructor an allocator will call is the copy-constructor when someone asks it to perform a placement-new , and the value passed to that copy-ctor has already been established somewhere else. 当有人要求它执行placement-new时 ,分配器将调用的唯一构造函数是copy-constructor ,并且传递给该copy-ctor的值已经在其他地方建立。

To sum things up; 总结一下; No, you cannot use an allocator so solve this particular problem. 不,你不能使用分配器,所以解决这个特殊的问题。


when does std::vector require the use of a default-ctor ? 什么时候std :: vector需要使用default-ctor

std::vector only uses the default-constructor of the type it holds in two situations: std::vector只使用它在两种情况下保存的类型的默认构造函数

  1. You specify the number of elements of your std::vector in the appropriate constructor overload but doesn't supply a default value 您在相应的构造函数重载中指定std::vector的元素数,但提供默认值

  2. You use std::vector<T>::resize (n) and increase the number of objects in the container (note the lack of specifying the 2nd argument to the member-function ) 你使用std::vector<T>::resize (n)并增加容器中的对象数量(注意缺少为成员函数指定第二个参数)


With the above in mind we can do plenty of things using the container without supplying a default constructor in our object, like initializing it to contain N elements of value X . 考虑到上述情况,我们可以使用容器做很多事情而不在我们的对象中提供默认构造函数,比如初始化它以包含值为X的 N个元素。

struct A{
  A (A const& a)
   : value_ (a.value_) 
  { } 

  A (int value)
    : value_ (value)
  {}  

  int value_;
};

int
main (int argc, char *argv[])
{
  std::vector<A> vec (5, A(1)); // initialize vector with 5 elements of A(1)

  vec.push_back (A(3));         // add another element
}

But I really want to be able to use vec.resize () !? 但我真的希望能够使用vec.resize()

Then you have two , three , four options: 然后你有 两个 三个 ,四个选项:

  1. Go with the C++11 approach of using Allocators 使用C ++ 11方法使用Allocators

  2. make your object have a default constructor 使您的对象具有默认构造函数

  3. wrap your object with a very thin wrapper who's only purpose is to default initialize the containing object (this might be easier said then done in some circumstances) 用一个非常薄的包装器包装你的对象,它的唯一目的是默认初始化包含的对象(在某些情况下这可能更容易说完)

  4. "Wrapping [the object] in boost::optional practically gives any type a default ctor" - @ Xeo “在boost :: optional中包装[对象]实际上给任何类型一个默认的ctor” - @ Xeo

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

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