STL common sequence container

Here is a brief description of STL containers commonly used to achieve the principle, the main points and so on. ...
vector
list
deque
stack
queue

Here is a brief description of STL containers commonly used to achieve the principle, the main points and so on.

vector

Vector is a common stl container. Its usage is similar to array. Its internal implementation is continuous space allocation. The difference between vector and array is that it can increase space elastically, while array is a static space, which cannot be expanded dynamically after allocation. The implementation of vecotr is relatively simple. The main key point is that when the space is insufficient, it will allocate 2 times of the current space, copy the old space data to the new space, and then delete the old space.

struct _Vector_impl: public _Tp_alloc_type { pointer _M_start; // Yuansu head pointer _M_finish; // Primordial tail pointer _M_end_of_storage; // Free space tail, // Omit part of code };

This is the code implementation of adding elements to the tail. You can see that if there is still space left at present, it will be added directly to the tail. If there is no space left, it will be expanded dynamically.

void push_back(const value_type& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) { _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, __x); ++this->_M_impl._M_finish; } else _M_realloc_insert(end(), __x); } template<typename _Tp, typename _Alloc> void vector<_Tp, _Alloc>::_M_realloc_insert(iterator __position, const _Tp& __x) { const size_type __len = _M_check_len(size_type(1), "vector::_M_realloc_insert"); // 2X current size const size_type __elems_before = __position - begin(); pointer __new_start(this->_M_allocate(__len)); pointer __new_finish(__new_start); __try { // The order of the three operations is dictated by the C++11 // case, where the moves could alter a new element belonging // to the existing vector. This is an issue only for callers // taking the element by lvalue ref (see last bullet of C++11 // [res.on.arguments]). _Alloc_traits::construct(this->_M_impl, __new_start + __elems_before, __x); __new_finish = pointer(); __new_finish = std::__uninitialized_move_if_noexcept_a(this->_M_impl._M_start, __position.base(), __new_start, _M_get_Tp_allocator(); ++__new_finish; __new_finish = std::__uninitialized_move_if_noexcept_a(__position.base(), this->_M_impl._M_finish, __new_finish, _M_get_Tp_allocator()); }__catch(...) { if (!__new_finish) _Alloc_traits::destroy(this->_M_impl, __new_start + __elems_before); else std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator()); _M_deallocate(__new_start, __len); __throw_exception_again; } std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, _M_get_Tp_allocator()); _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage - this->_M_impl._M_start); this->_M_impl._M_start = __new_start; this->_M_impl._M_finish = __new_finish; this->_M_impl._M_end_of_storage = __new_start + __len; } // Called by _M_fill_insert, _M_insert_aux etc. size_type _M_check_len(size_type __n, const char* __s) const { if (max_size() - size() < __n) __throw_length_error(__N(__s)); const size_type __len = size() + std::max(size(), __n); // Double length return (__len < size() || __len > max_size()) ? max_size() : __len; } pointer _M_allocate(size_t __n) { typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr; return __n != 0 ? _Tr::allocate(_M_impl, __n) : pointer(); }

You can use [] when using because it implements overload [].

reference operator[](size_type __n) _GLIBCXX_NOEXCEPT { __glibcxx_requires_subscript(__n); return *(this->_M_impl._M_start + __n); }

When using, we should pay attention to the problem of iterator failure, which is a problem in many STL containers.

list

Unlike vector, the list of linked lists does not support random access because the elements are not allocated consecutively in memory. The advantage is that the time complexity of insertion and deletion is O(1). In STL, its implementation is a two-way linked list, the definition of its nodes can see that there are precursor and successor pointers, and the implementation is relatively simple.

/// An actual node in the %list. template<typename _Tp> struct _List_node : public __detail::_List_node_base { _Tp _M_data; _Tp* _M_valptr() { return std::__addressof(_M_data); } _Tp const* _M_valptr() const { return std::__addressof(_M_data); } }; struct _List_node_base { _List_node_base* _M_next; _List_node_base* _M_prev; static void swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT; void _M_transfer(_List_node_base* const __first, _List_node_base* const __last) _GLIBCXX_USE_NOEXCEPT; void _M_reverse() _GLIBCXX_USE_NOEXCEPT; void _M_hook(_List_node_base* const __position) _GLIBCXX_USE_NOEXCEPT; void _M_unhook() _GLIBCXX_USE_NOEXCEPT; };

deque

The two terminal queue is different from vector and list. It is a small segment of continuous space. Each segment of continuous space is connected in series by a pointer array (the head pointer of each continuous space array is stored in this array), so that all elements can be accessed. The reason for adopting this storage layout is that there are application scenarios. After analyzing the source code, we can understand why it does this.

deque source code analysis

Let's take part of the source code to see its implementation details. The implementation code of the iterator of the two terminal queue is as follows (compared with vector and list, the access to the elements needs special treatment at the edge of each continuous space allocation because of the different storage layout):

#define _GLIBCXX_DEQUE_BUF_SIZE 512 / / default continuous space size _GLIBCXX_CONSTEXPR inline size_t __deque_buf_size(size_t __size) { return (__size < _GLIBCXX_DEQUE_BUF_SIZE ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) :size_t(1)); } template<typename _Tp, typename _Ref, typename _Ptr> struct _Deque_iterator { typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator; typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator; typedef _Tp* _Elt_pointer; typedef _Tp** _Map_pointer; static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT { return __deque_buf_size(sizeof(_Tp)); } typedef std::random_access_iterator_tag iterator_category; typedef _Tp value_type; typedef _Ptr pointer; typedef _Ref reference; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef _Deque_iterator _Self; _Elt_pointer _M_cur; // current location _Elt_pointer _M_first; // The beginning of each small space _Elt_pointer _M_last; // The end of each small space _Map_pointer _M_node; // Pointer array, where you can access all contiguous memory segments // Constructor _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT : _M_cur(__x), _M_first(*__y), _M_last(*__y + _S_buffer_size()), _M_node(__y) { } _Deque_iterator() _GLIBCXX_NOEXCEPT: _M_cur(), _M_first(), _M_last(), _M_node() { } _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT: _M_cur(__x._M_cur), _M_first(__x._M_first), _M_last(__x._M_last), _M_node(__x._M_node) { } iterator _M_const_cast() const _GLIBCXX_NOEXCEPT { return iterator(_M_cur, _M_node); // Returns the current element iterator } reference operator*() const _GLIBCXX_NOEXCEPT { return *_M_cur; } pointer operator->() const _GLIBCXX_NOEXCEPT { return _M_cur; } // Overloading + + operators, you can see that when_ M_ When cur points to the end of this continuous space, the first address of the next continuous space is used to access the next element _Self& operator++() _GLIBCXX_NOEXCEPT { ++_M_cur; if (_M_cur == _M_last) { _M_set_node(_M_node + 1); // Move to next contiguous storage space _M_cur = _M_first; // The first element of the next continuous space } return *this; } _Self operator++(int) _GLIBCXX_NOEXCEPT { _Self __tmp = *this; ++*this; return __tmp; } _Self& operator--() _GLIBCXX_NOEXCEPT { if (_M_cur == _M_first) { // Similar to + +, if it is the first element at present, -, it should be adjusted to the previous continuous storage space _M_set_node(_M_node - 1); _M_cur = _M_last; // Move to the end of the previous space, } --_M_cur; // Because it's a [start, last] interval, here we need to--_ M_cur; return *this; } _Self operator--(int) _GLIBCXX_NOEXCEPT { _Self __tmp = *this; --*this; return __tmp; } _Self& operator+=(difference_type __n) _GLIBCXX_NOEXCEPT { const difference_type __offset = __n + (_M_cur - _M_first); if (__offset >= 0 && __offset < difference_type(_S_buffer_size())) // If the current continuous space satisfies _M_cur += __n; else { // If the continuous space of the current segment is not enough, the calculation needs to jump to the continuous space const difference_type __node_offset = __offset > 0 ? __offset / difference_type(_S_buffer_size()) : -difference_type((-__offset - 1) / _S_buffer_size()) - 1; _M_set_node(_M_node + __node_offset); _M_cur = _M_first + (__offset - __node_offset * difference_type(_S_buffer_size())); } return *this; } _Self operator+(difference_type __n) const _GLIBCXX_NOEXCEPT { _Self __tmp = *this; return __tmp += __n; } _Self& operator-=(difference_type __n) _GLIBCXX_NOEXCEPT { return *this += -__n; } _Self operator-(difference_type __n) const _GLIBCXX_NOEXCEPT { _Self __tmp = *this; return __tmp -= __n; } reference operator[](difference_type __n) const _GLIBCXX_NOEXCEPT { return *(*this + __n); } // Prepares to traverse new_node. Sets everything except _M_cur, which should therefore be set by the caller immediately afterwards, based on _M_first and _M_last. void _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT { // Jump to a new continuous storage space _M_node = __new_node; _M_first = *__new_node; _M_last = _M_first + difference_type(_S_buffer_size()); } };

From the implementation of the above deque iterator, the main thing to pay attention to is the edge of each continuous space. After looking at the iterator, let's take a look at the implementation code of the deque class. Here we delete most of the code and keep some of the code. Focus on the most commonly used push in deque_ front,pop_front and push_back,pop_ The implementation of back. push_back time complexity O(1) is easy to understand, the process is similar to vector, but push_ Why is front also O(1)? If you insert an element in the header, and the first continuous space is still free from the start, just insert it directly. If there is no space left, create a new continuous space, and put the first address in the map. If there is no space for the map to place the first address, adjust the map, and then insert the first address. For details, see the specific implementation of the source code:

template<typename _Tp, typename _Alloc = std::allocator<_Tp> > class deque : protected _Deque_base<_Tp, _Alloc> { typedef _Deque_base<_Tp, _Alloc> _Base; typedef typename _Base::_Tp_alloc_type _Tp_alloc_type; typedef typename _Base::_Alloc_traits _Alloc_traits; typedef typename _Base::_Map_pointer _Map_pointer; public: typedef _Tp value_type; typedef typename _Alloc_traits::pointer pointer; typedef typename _Alloc_traits::const_pointer const_pointer; typedef typename _Alloc_traits::reference reference; typedef typename _Alloc_traits::const_reference const_reference; typedef typename _Base::iterator iterator; typedef typename _Base::const_iterator const_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef _Alloc allocator_type; protected: static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT { return __deque_buf_size(sizeof(_Tp)); } // Functions controlling memory layout, and nothing else. using _Base::_M_initialize_map; using _Base::_M_create_nodes; using _Base::_M_destroy_nodes; using _Base::_M_allocate_node; using _Base::_M_deallocate_node; using _Base::_M_allocate_map; using _Base::_M_deallocate_map; using _Base::_M_get_Tp_allocator; /** * A total of four data members accumulated down the hierarchy. * May be accessed via _M_impl.* */ using _Base::_M_impl; public: // Omit constructors and destructors /* * @brief Assigns a given value to a %deque. * @param __n Number of elements to be assigned. * @param __val Value to be assigned. * * This function fills a %deque with @a n copies of the given * value. Note that the assignment completely changes the * %deque and that the resulting %deque's size is the same as * the number of elements assigned. */ void assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); } // Omit other assign overloaded functions /// Get a copy of the memory allocation object. allocator_type get_allocator() const _GLIBCXX_NOEXCEPT{ return _Base::get_allocator(); } // iterators /** * Returns a read/write iterator that points to the first element in the * %deque. Iteration is done in ordinary element order. */ iterator begin() _GLIBCXX_NOEXCEPT { return this->_M_impl._M_start; } const_iterator begin() const _GLIBCXX_NOEXCEPT { return this->_M_impl._M_start; } /** * Returns a read/write iterator that points one past the last * element in the %deque. Iteration is done in ordinary * element order. */ iterator end() _GLIBCXX_NOEXCEPT{ return this->_M_impl._M_finish; } const_iterator end() const _GLIBCXX_NOEXCEPT { return this->_M_impl._M_finish; } // Omit other iterator related code // [23.2.1.2] capacity /** Returns the number of elements in the %deque. */ size_type size() const _GLIBCXX_NOEXCEPT { return this->_M_impl._M_finish - this->_M_impl._M_start; } /** Returns the size() of the largest possible %deque. */ size_type max_size() const _GLIBCXX_NOEXCEPT { return _Alloc_traits::max_size(_M_get_Tp_allocator()); } /** * @brief Resizes the %deque to the specified number of elements. * @param __new_size Number of elements the %deque should contain. * * This function will %resize the %deque to the specified * number of elements. If the number is smaller than the * %deque's current size the %deque is truncated, otherwise * default constructed elements are appended. */ void resize(size_type __new_size) { const size_type __len = size(); if (__new_size > __len) _M_default_append(__new_size - __len); else if (__new_size < __len) _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size)); } #if __cplusplus >= 201103L /** A non-binding request to reduce memory use. */ void shrink_to_fit() noexcept { _M_shrink_to_fit(); } #endif /** * Returns true if the %deque is empty. (Thus begin() would * equal end().) */ bool empty() const _GLIBCXX_NOEXCEPT { return this->_M_impl._M_finish == this->_M_impl._M_start; } // element access /** * @brief Subscript access to the data contained in the %deque. * @param __n The index of the element for which data should be * accessed. * @return Read/write reference to data. * * This operator allows for easy, array-style, data access. * Note that data access with this operator is unchecked and * out_of_range lookups are not defined. (For checked lookups * see at().) */ reference operator[](size_type __n) _GLIBCXX_NOEXCEPT { __glibcxx_requires_subscript(__n); return this->_M_impl._M_start[difference_type(__n)]; } protected: /// Safety check used only from at(). void _M_range_check(size_type __n) const { if (__n >= this->size()) __throw_out_of_range_fmt(__N("deque::_M_range_check: __n " "(which is %zu)>= this->size() " "(which is %zu)"), __n, this->size()); } public: /** * @brief Provides access to the data contained in the %deque. * @param __n The index of the element for which data should be * accessed. * @return Read/write reference to data. * @throw std::out_of_range If @a __n is an invalid index. * * This function provides for safer data access. The parameter * is first checked that it is in the range of the deque. The * function throws out_of_range if the check fails. */ reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; } /** * @brief Provides access to the data contained in the %deque. * @param __n The index of the element for which data should be * accessed. * @return Read-only (constant) reference to data. * @throw std::out_of_range If @a __n is an invalid index. * * This function provides for safer data access. The parameter is first * checked that it is in the range of the deque. The function throws * out_of_range if the check fails. */ const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; } /** * Returns a read/write reference to the data at the first * element of the %deque. */ reference front() _GLIBCXX_NOEXCEPT { __glibcxx_requires_nonempty(); return *begin(); } /** * Returns a read/write reference to the data at the last element of the * %deque. */ reference back() _GLIBCXX_NOEXCEPT { __glibcxx_requires_nonempty(); iterator __tmp = end(); --__tmp; return *__tmp; } /** * @brief Add data to the front of the %deque. * @param __x Data to be added. * * This is a typical stack operation. The function creates an * element at the front of the %deque and assigns the given * data to it. Due to the nature of a %deque this operation * can be done in constant time. */ void push_front(const value_type& __x) { // If there is space left in the first continuous space header, insert the element directly if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first) { _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_start._M_cur - 1, __x); --this->_M_impl._M_start._M_cur; } else // If not, reallocate space at the front _M_push_front_aux(__x); } /** * @brief Add data to the end of the %deque. * @param __x Data to be added. * * This is a typical stack operation. The function creates an * element at the end of the %deque and assigns the given data * to it. Due to the nature of a %deque this operation can be * done in constant time. */ void push_back(const value_type& __x) { if (this->_M_impl._M_finish._M_cur != this->_M_impl._M_finish._M_last - 1) { _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish._M_cur, __x); ++this->_M_impl._M_finish._M_cur; } else _M_push_back_aux(__x); } /** * @brief Removes first element. * * This is a typical stack operation. It shrinks the %deque by one. * * Note that no data is returned, and if the first element's data is * needed, it should be retrieved before pop_front() is called. */ void pop_front() _GLIBCXX_NOEXCEPT { __glibcxx_requires_nonempty(); if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_last - 1) { _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_start._M_cur); ++this->_M_impl._M_start._M_cur; } else _M_pop_front_aux(); } /** * @brief Removes last element. * * This is a typical stack operation. It shrinks the %deque by one. * * Note that no data is returned, and if the last element's data is * needed, it should be retrieved before pop_back() is called. */ void pop_back() _GLIBCXX_NOEXCEPT { __glibcxx_requires_nonempty(); if (this->_M_impl._M_finish._M_cur != this->_M_impl._M_finish._M_first) { --this->_M_impl._M_finish._M_cur; _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish._M_cur); } else _M_pop_back_aux(); } /** * @brief Inserts given value into %deque before specified iterator. * @param __position An iterator into the %deque. * @param __x Data to be inserted. * @return An iterator that points to the inserted data. * * This function will insert a copy of the given value before the * specified location. */ iterator insert(iterator __position, const value_type& __x); /** * Erases all the elements. Note that this function only erases the * elements, and that if the elements themselves are pointers, the * pointed-to memory is not touched in any way. Managing the pointer is * the user's responsibility. */ void clear() _GLIBCXX_NOEXCEPT { _M_erase_at_end(begin()); } protected: // Internal constructor functions follow. // Omit some codes void _M_push_back_aux(const value_type&); void _M_push_front_aux(const value_type&); void _M_pop_back_aux(); void _M_pop_front_aux(); // Omit some codes };

The implementation of deque is much more complex than vector and list, mainly because its spatial layout is not the same. The following code is mainly for the operation of two end queue head and tail (push_front,push_back,pop_front,pop_back) involves some code implementation of spatial change:

// Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1. template<typename _Tp, typename _Alloc> void deque<_Tp, _Alloc>::_M_push_back_aux(const value_type& __t) { _M_reserve_map_at_back(); *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node(); // map new pointer points to newly allocated continuous space __try { this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t); this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node + 1); this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first; } __catch(...) { _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1)); __throw_exception_again; } } // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first. template<typename _Tp, typename _Alloc> void deque<_Tp, _Alloc>::_M_push_front_aux(const value_type& __t) { _M_reserve_map_at_front(); *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node(); // map specifies the location to point to the newly allocated contiguous space __try { this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node - 1); this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1; this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t); } __catch(...) { ++this->_M_impl._M_start; _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1)); __throw_exception_again; } } // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first. template <typename _Tp, typename _Alloc> void deque<_Tp, _Alloc>::_M_pop_back_aux() { _M_deallocate_node(this->_M_impl._M_finish._M_first); this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1); this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1; _Alloc_traits::destroy(_M_get_Tp_allocator(), this->_M_impl._M_finish._M_cur); } // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1. // Note that if the deque has at least one element (a precondition for this // member function), and if // _M_impl._M_start._M_cur == _M_impl._M_start._M_last, // then the deque must have at least two nodes. template <typename _Tp, typename _Alloc> void deque<_Tp, _Alloc>::_M_pop_front_aux() { _Alloc_traits::destroy(_M_get_Tp_allocator(), this->_M_impl._M_start._M_cur); _M_deallocate_node(this->_M_impl._M_start._M_first); this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1); this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first; }

The original code below is to adjust the map. If the map does not have the appropriate space to insert the new first address of the continuous space, the map will be redistributed (for example, the back of the map is full, but the front is still a lot of empty, so it is necessary to move the elements in the current map, so that the elements of the map are distributed in the middle, and the first and last ends are free, so as to insert new elements later; If the map space is insufficient, a new map space needs to be allocated, and the new space size is larger than the number of new pointer elements + 2).

void _M_reserve_map_at_back(size_type __nodes_to_add = 1) { if (__nodes_to_add + 1 > this->_M_impl._M_map_size - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map)) _M_reallocate_map(__nodes_to_add, false); } void _M_reserve_map_at_front(size_type __nodes_to_add = 1) { if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node - this->_M_impl._M_map)) _M_reallocate_map(__nodes_to_add, true); } template <typename _Tp, typename _Alloc> void deque<_Tp, _Alloc>::_M_reallocate_map(size_type __nodes_to_add, bool __add_at_front) { const size_type __old_num_nodes = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1; const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add; _Map_pointer __new_nstart; if (this->_M_impl._M_map_size > 2 * __new_num_nodes) { __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size - __new_num_nodes) / 2 + (__add_at_front ? __nodes_to_add : 0); // Here, the start of the new map moves back for a period of time, in order to have more space when inserting in the front in the future, as well as a period of space left in the back. if (__new_nstart < this->_M_impl._M_start._M_node) std::copy(this->_M_impl._M_start._M_node, this->_M_impl._M_finish._M_node + 1, __new_nstart); else std::copy_backward(this->_M_impl._M_start._M_node, this->_M_impl._M_finish._M_node + 1, __new_nstart + __old_num_nodes); } else { size_type __new_map_size = this->_M_impl._M_map_size + std::max(this->_M_impl._M_map_size, __nodes_to_add) + 2; // There should be at least 2 free places _Map_pointer __new_map = this->_M_allocate_map(__new_map_size); __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2 + (__add_at_front ? __nodes_to_add : 0); std::copy(this->_M_impl._M_start._M_node, this->_M_impl._M_finish._M_node + 1, __new_nstart); _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size); this->_M_impl._M_map = __new_map; this->_M_impl._M_map_size = __new_map_size; } this->_M_impl._M_start._M_set_node(__new_nstart); this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1); }

More detailed or make complaints about the source code of STL, and the source code of STL is too thick. It looks too tired. If you implement a mini version of STL according to its implementation principle, it should be a lot simpler and more concise.

So far, the core source code of deque has been basically analyzed, and it also basically shows how several key member functions in deque are implemented, the implementation of its iterator, and the implementation and adjustment of its map.

Comparison of deque with vector and list

Vector can realize random access and dynamic expansion, but it needs to copy all the elements when inserting O(n) in the header, which is also inefficient. The efficiency of inserting and deleting head and tail elements in list is very high (O(n), but it can't be accessed randomly. The efficiency of searching is O(n). Each node needs to store the front and back node pointers, which has a large additional storage overhead. Deque is equal to balancing the advantages and disadvantages of the two containers. It is efficient to insert and delete elements in the end. It is almost the same to insert O(n) in the middle, but it can realize random access. In dynamic expansion, it does not need to copy all elements, only needs to allocate enough continuous storage space, and at most, it needs to copy the map to the new map again. Map is each continuous storage The first address pointer array of space has a very small capacity compared with all elements and a very small price in the era of dynamic expansion. Therefore, deque has higher performance than vector in more general cases, and has less additional storage space than list (but deque has a larger minimum memory cost, because it needs map and a continuous storage space cost, that is, when the number of elements is very small, the cost is greater than list, but when the number of elements is large, the space cost is less than list).

stack

Stack is also a frequently used data structure. Its implementation is relatively simple, and its internal implementation depends on deque. Of course, it can also be implemented with vector and list.

// Stack implementation -*- C++ -*- template<typename _Tp, typename _Sequence = deque<_Tp> > class stack { // concept requirements typedef typename _Sequence::value_type _Sequence_value_type; public: typedef typename _Sequence::value_type value_type; typedef typename _Sequence::reference reference; typedef typename _Sequence::const_reference const_reference; typedef typename _Sequence::size_type size_type; typedef _Sequence container_type; protected: _Sequence c; public: stack(): c() { } // Omit constructors and destructors /** * Returns true if the %stack is empty. */ bool empty() const { return c.empty(); } /** Returns the number of elements in the %stack. */ size_type size() const { return c.size(); } /** * Returns a read/write reference to the data at the first * element of the %stack. */ reference top() { __glibcxx_requires_nonempty(); return c.back(); } /** * @brief Add data to the top of the %stack. * @param __x Data to be added. * * This is a typical %stack operation. The function creates an * element at the top of the %stack and assigns the given data * to it. The time complexity of the operation depends on the * underlying sequence. */ void push(const value_type& __x) { c.push_back(__x); } /** * @brief Removes first element. * * This is a typical %stack operation. It shrinks the %stack * by one. The time complexity of the operation depends on the * underlying sequence. * * Note that no data is returned, and if the first element's * data is needed, it should be retrieved before pop() is * called. */ void pop() { __glibcxx_requires_nonempty(); c.pop_back(); } // Omit other non critical code };

queue

There are ordinary first in, first out queues and priority queues. Priority queues should not only be in order, but also focus on the high priority first out queues.

Implementation of common queue

The implementation of common queue is similar to that of stack, which is also based on deque.

template<typename _Tp, typename _Sequence = deque<_Tp> > class queue { // concept requirements typedef typename _Sequence::value_type _Sequence_value_type; public: typedef typename _Sequence::value_type value_type; typedef typename _Sequence::reference reference; typedef typename _Sequence::const_reference const_reference; typedef typename _Sequence::size_type size_type; typedef _Sequence container_type; protected: /* Maintainers wondering why this isn't uglified as per style * guidelines should note that this name is specified in the standard, * C++98 [23.2.3.1]. * (Why? Presumably for the same reason that it's protected instead * of private: to allow derivation. But none of the other * containers allow for derivation. Odd.) */ /// @c c is the underlying container. _Sequence c; public: queue(): c() { } // Omit constructors and destructors bool empty() const { return c.empty(); } size_type size() const { return c.size(); } reference front() { __glibcxx_requires_nonempty(); return c.front(); } reference back() { __glibcxx_requires_nonempty(); return c.back(); } // Add data to the end of the %queue. void push(const value_type& __x) { c.push_back(__x); } // Removes first element. void pop() { __glibcxx_requires_nonempty(); c.pop_front(); } };

priority_queue implementation

The implementation principle of priority queue is based on heap. The bottom layer of heap is array, so priority here_ The sequence container at the bottom of the queue is vector. If you select vector instead of other containers, it is because the priority queue is based on the heap. In various operations of the heap, insert, delete, insert from the tail and delete. In fact, the tail element is physically deleted at the end. Moreover, its capacity expansion is twice that of the previous one, which conforms to the fact that the number of nodes at the next level of the binary tree is + 1, and the capacity expansion is exactly twice that of the previous one It's good. Of course, other containers can also be used (such as deque, but it's not optimal). As for the principle of heap implementation priority queue, it will not be described here. The source code is as follows:

template<typename _Tp, typename _Sequence = vector<_Tp>, typename _Compare = less<typename _Sequence::value_type> > class priority_queue { #ifdef _GLIBCXX_CONCEPT_CHECKS // concept requirements typedef typename _Sequence::value_type _Sequence_value_type; # if __cplusplus < 201103L __glibcxx_class_requires(_Tp, _SGIAssignableConcept) # endif __glibcxx_class_requires(_Sequence, _SequenceConcept) __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept) __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp, _BinaryFunctionConcept) #endif #if __cplusplus >= 201103L template<typename _Alloc> using _Uses = typename enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; #endif public: typedef typename _Sequence::value_type value_type; typedef typename _Sequence::reference reference; typedef typename _Sequence::const_reference const_reference; typedef typename _Sequence::size_type size_type; typedef _Sequence container_type; typedef _Compare value_compare; protected: _Sequence c; _Compare comp; // Priority queues are based on the heap, which often requires comparison operations public: // * @brief Default constructor creates no elements. explicit priority_queue(const _Compare& __x = _Compare(), const _Sequence& __s = _Sequence()): c(__s), comp(__x) { std::make_heap(c.begin(), c.end(), comp); // Structural pile } // Omit other constructors /** * Returns true if the %queue is empty. */ bool empty() const { return c.empty(); } /** Returns the number of elements in the %queue. */ size_type size() const { return c.size(); } /** * Returns a read-only (constant) reference to the data at the first * element of the %queue. */ const_reference top() const { __glibcxx_requires_nonempty(); return c.front(); } /** * @brief Add data to the %queue. * @param __x Data to be added. * * This is a typical %queue operation. * The time complexity of the operation depends on the underlying * sequence. */ void push(const value_type& __x) { // Insert elements into the priority queue, put them at the end of the container, and then "move up" to make them meet the heap property. c.push_back(__x); std::push_heap(c.begin(), c.end(), comp); } /** * @brief Removes first element. * * This is a typical %queue operation. It shrinks the %queue * by one. The time complexity of the operation depends on the * underlying sequence. * * Note that no data is returned, and if the first element's * data is needed, it should be retrieved before pop() is * called. */ void pop() { //Pop the first element from the priority queue __glibcxx_requires_nonempty(); std::pop_heap(c.begin(), c.end(), comp); c.pop_back(); } };

It can be seen that as long as the implementation principle of heap is understood, the implementation principle of priority queue is very easy to understand, and the STL source code analysis of heap is not continued here.

6 June 2020, 04:05 | Views: 9128

Add new comment

For adding a comment, please log in
or create account

0 comments