簡體   English   中英

如何在c ++ stl中實現雙端隊列

[英]How is deque implemented in c++ stl

我只是想知道如何實現雙端隊列,以及在該實現中如何提供諸如push_front和random access operator之類的基本操作

我只是想知道如何執行雙端隊列

借口做ASCII藝術總是很好的:

+-------------------------------------------------------------+                       
| std::deque<int>                                             |                       
|                                                             |                       
|  subarrays:                                                 |                       
| +---------------------------------------------------------+ |                       
| |           |           |           |         |           | |                       
| | int(*)[8] | int(*)[8] | int(*)[8] |int(*)[8]|int(*)[8]  | |                       
| |           |           |           |         |           | |                       
| +---------------------------------------------------------+ |                       
|                        /            \                       |                       
|                       /              \                      |                       
|                      /                \                     |                       
|                     /                  \                    |                       
|                    /                    \                   |                       
|                   /                      \                  |                       
|                  /                        \                 |                       
|                 /                          \                |                       
|                -                            -               |                       
|               +------------------------------+              |                       
|               | ?, ?, 42, 43, 50, ?, ?, ?, ? |              |                       
|               +------------------------------+              |                       
|                                                             |
| additional state:                                           |                       
|                                                             |                       
| - pointer to begin of the subarrays                         |                       
| - current capacity and size                                 |                       
| - pointer to current begin and end                          |                       
+-------------------------------------------------------------+                       

該實現中如何提供諸如push_front和random access operator之類的基本操作?

首先,來自libcxx的std::deque::push_front

template <class _Tp, class _Allocator>
void
deque<_Tp, _Allocator>::push_front(const value_type& __v)
{
    allocator_type& __a = __base::__alloc();
    if (__front_spare() == 0)
        __add_front_capacity();
    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
    --__base::__start_;
    ++__base::size();
}

顯然,這檢查了已經分配在前端的內存是否可以容納其他元素。 如果沒有,它將分配。 然后,主要工作轉移到迭代器: _VSTD::addressof(*--__base::begin())在容器的當前前元素之前_VSTD::addressof(*--__base::begin())一個位置,並將此地址傳遞給分配器以構造一個新的元素通過復制v到位(默認分配器肯定會進行new放置)。

現在隨機訪問。 同樣從libcxx開始, std::deque::operator[] (非const版本)是

template <class _Tp, class _Allocator>
inline
typename deque<_Tp, _Allocator>::reference
deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
{
    size_type __p = __base::__start_ + __i;
    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
}

這幾乎可以計算相對於某個起始索引的索引,然后確定子數組和相對於子數組起始的索引。 __base::__block_size應該是一個子__base::__block_size的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM