Yes, there is a way. Use linear search to find the element of the deque whose address is the same as the pointer. This should produce an iterator to the element that you want to remove. Pass that to deque::erase. There is a standard algorithm for linear search: std::find_if.
Yes, there is a way. Use linear search to find the element of the deque whose address is the same as the pointer. This should produce an iterator to the element that you want to remove. Pass that to
deque::erase
. There is a standard algorithm for linear search:std::find_if
.但是请注意,此搜索会产生一些开销。如果您首先将迭代器而不是指针存储,则可以避免搜索。也就是说,擦除操作本身具有线性复杂度(除非元素位于两端),因此搜索不会使复杂度渐近恶化。
还要注意,擦除双端队列的元素会使所有引用(包括指向容器的指针和迭代器)无效,除非您从两端之一进行擦除(在这种情况下,仅对已擦除元素的引用无效)。