Converting between vectors with different allocators

前端 未结 3 823
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 09:33

I have written a simple C++11 style stateful allocator type. Given

template class my_allocator {
   // the usual stuff
};

template

        
3条回答
  •  孤城傲影
    2021-02-08 10:09

    There are 3 solutions to your problem:

    I. If your vector 'y' does not exist yet, you could use :

    std::vector y(x.begin(), x.end());
    

    II. If your vector 'y' already exist, you could use : (e.g. for a member of a class)

    this->y.assign(x.begin(), x.end());
    

    These 2 ways to fill a vector are not looking the container type. (and therefore the allocator type)

    III. Another solution is to create a parent class which could be used as an allocator adapter between the std allocator and your own allocator. In this case, your 2 allocators are of the same type, and could be used with vector methods like operator=().

提交回复
热议问题