deque::insert() 在索引处?
如何在线性时间内 insert()
将一堆项目插入到 deque
的中间?
(我插入的项目无法通过 STL 样式迭代器访问。)
How do I insert()
a bunch of items to the middle of a deque
in linear time?
(The items I am inserting are not accessible through an STL-style iterator.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个
deque::insert(iterator pos, const T&x)
函数,将位置pos
作为deque::iterator
和一个单一的元素。使用这种方法,您可以一一插入所有元素。pos
可以通过deque.begin()+index
轻松获得(如果您有一个要在其之前插入元素的索引)。insert
方法为新插入的元素返回一个迭代器,只需增加此返回的迭代器即可获取下一个位置:但是,这可能需要
O(n*k)
时间,因为插入单个元素的 (iirc) 是线性时间运算 iirc。第二个重载是 deque::insert(iterator pos, InputIterator f, InputIterator l):请记住,简单指针也满足 STL 输入迭代器的要求,因此如果您有一个 C 样式数组 < code>T array[] 长度为
n
包含您的元素,您可以插入此数组中的所有元素此操作可以在线性时间内执行,即
O(n +k)
。我不确定标准是否保证这一点,但我认为大多数实现都会有效地做到这一点。编辑
我快速检查了微软的实现,他们通过
push_back
或push_front
序列来实现,无论什么更接近pos< /code> 然后将元素旋转到最终位置,这保证了上述
O(n+k)
复杂度。当然,这也可以“手动”完成,例如:(我从 Microsoft 的
deque::insert
实现中复制了代码,删除了调试代码和异常处理,There is a
deque::insert(iterator pos, const T&x)
function taking the positionpos
asdeque::iterator
and a single element. Using this method you could insert all elements one by one.pos
can easily be obtained (if you have an index before which you want to insert the element) bydeque.begin()+index
. Theinsert
method returns an iterator for the newly inserted element, simply increment this returned iterator to get the next position:This however cantake
O(n*k)
time, since insertion of a single element is (iirc) a linear time operation iirc.The second overload is
deque::insert(iterator pos, InputIterator f, InputIterator l)
: Remember that simple pointers also fulfill the requirements of an STL input iterator, so if you have a C-Style arrayT array[]
of lengthn
containing your elements, you could insert all elements from this array withThis operation can be carried out in linear time, i.e.
O(n+k)
. I'm not sure if this is guaranteed by the standard, but I suppose that most implementation will do it efficiently.EDIT
I quickly checked with Microsoft's implementation, they do it by a sequence of either
push_back
orpush_front
, whatever is closer topos
and then rotating the elements to their final place, which guarantees the aboveO(n+k)
complexity. Of course, that could also be done "by hand" like:(I copied the code from Microsofts implementation of
deque::insert
, removing debug code and exception handling,调用需要插入一系列项目的插入方法,请参阅此处列出的第三种方法:
http://msdn.microsoft.com/en-us/library/zcww84w5(v=vs.71).aspx
并且,创建您自己的 STL 样式迭代器来访问您想要插入的项目。请参阅:
C++ 中的自定义迭代器
Call the insert method that takes a sequence of items to insert, see the 3rd method listed here:
http://msdn.microsoft.com/en-us/library/zcww84w5(v=vs.71).aspx
And, create your own STL-style iterator to access the items you want to insert. See:
Custom Iterator in C++
输入:
双端队列:lengtl = l,
新项目(m = 新项目的数量)
算法:
创建一个新的双端队列 (1)
复制原始双端队列中的所有项目,直到您所在的位置想要插入新的 (p)
添加新的项目 (m)
从旧的双端队列中添加项目 (mp)
也许您可以只使用新的双端队列,但最坏的情况是:
将新的双端队列复制到旧的双端队列上(完全清除后:):
成本(l+m)
最坏的成本是因此: origsize * 2 + newitems 是线性的。
这里不计算“干净的牌组”,但它也是线性的(最坏的情况下)。
Input:
Deque: lengtl = l,
New items (m = number of new items)
Algo:
create a new deque (1)
Copy all items from original deque until where you want to insert the new ones (p)
Add new items (m)
Add items from old deque (m-p)
Maybe you can just use the new deque but at worst:
Copy new deque onto old one (after a complete clear: ):
Cost (l+m)
The worst cost is thus: origsize * 2 + newitems which is linear.
The "clear deck" isn't counted here but it is also linear ( at worst).
将插入点之后的所有元素添加到向量中。
删除插入点之后的所有元素。
将新范围附加到双端队列。
将向量附加到双端队列。
这是 O(2n) 最坏情况,而不是 O(n^2)。
Add all the elements after the insertion point to a vector.
Remove all elements after insertion point.
Append new range to deque.
Append vector to deque.
This is O(2n) worst case, instead of O(n^2).