c++ - 使用 std::deque 时重定位赋值

标签 c++ stl deque

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/

相关文章:

python - 如何在尺寸有限的结构中保存有序的独特元素?

c++ - 从 vector 切换到双端队列的大小限制通常是多少?

java - 为什么双端队列(ArrayDeque)容量是2的幂?

c++ - Eigen 和巨大的密集二维阵列

c++ - 堆栈上的匿名对象,在 C++ 中?

c++ - 如何使用/安装自定义 Directshow 过滤器

c++ - 如何与 C++ 程序共享 Python 中的数组?

c++ - 具有结构的 STL map

c++ - 如何为采用 STL 容器迭代器的函数提供函数签名?

c++ - 创建自定义 std::streambuf