Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relocation on assignment when using a std::deque

Tags:

c++

stl

deque

Does the C++ standard guarantee that when one pushes back a new element into a deque, none of the pre-existing elements will be relocated to a new memory address?

like image 794
doron Avatar asked Dec 13 '25 08:12

doron


1 Answers

Yes, the standard provides such a guarantee:

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.

Absence of effect on references in fact mean that elements will not be relocated.

According to the C++11 working draft N3242=11-0012

like image 188
Lol4t0 Avatar answered Dec 15 '25 12:12

Lol4t0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!