C++ 标准是否保证当将新元素推回双端队列时,不会将任何预先存在的元素重新定位到新的内存地址?
最佳答案
是的,标准提供了这样的保证:
23.3.3.4 deque modifiers [lib.deque.modifiers]
iterator insert(const_iterator position, const T& x); iterator insert(const_iterator position, T&& x); iterator insert(const_iterator position, size_type n, const T& x); template <class InputIterator> iterator insert(const_iterator position, InputIterator first, InputIterator last); iterator insert(const_iterator position, initializer_list<T>); template <class... Args> void emplace_front(Args&&... args); template <class... Args> void emplace_back(Args&&... args); template <class... Args> iterator emplace(const_iterator position, Args&&... args); void push_front(const T& x); void push_front(T&& x); void push_back(const T& x); void push_back(T&& x);
1. Effects: An insert in the middle of the deque invalidates all the iterators and references to elements of the deque. An insert at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.
对引用没有影响实际上意味着元素不会被重新定位。
根据 C++11 工作草案 N3242=11-0012
关于c++ - 使用 std::deque 时重定位赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17003335/