简体   繁体   English

如何为我的自定义向量类实现类似 vector.insert 的方法?

[英]How can i implement a vector.insert like method for my custom vector class?

i have made a custom class to implement a std::vector.我制作了一个自定义类来实现 std::vector。
My class has Forward iterator support.我的班级有前向迭代器支持。 I'd like to know how I can implement a vector.insert like method that allows me to insert an element at any position inside the vector.我想知道如何实现类似 vector.insert 的方法,该方法允许我在向量内的任何位置插入元素。
I can't use allocator, but i'd like to know if the insert method can be done with iterators and, if it is possible, how can it be done?我不能使用分配器,但我想知道插入方法是否可以用迭代器完成,如果可能,如何完成?

To implement insert you need to move/copy all elements post the insertion point one element (possibly allocating new memory and moving all members if your internal storage is not large enough), then insert the new element in the "hole" you just made.要实现insert您需要移动/复制插入点后的所有元素一个元素(如果您的内部存储空间不够大,可能会分配新内存并移动所有成员),然后将新元素插入您刚刚制作的“洞”中。

Note that this (of course) invalidates existing iterators.请注意,这(当然)会使现有的迭代器无效。

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

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