小编Pet*_*ran的帖子

deque如何具有摊销的常数时间复杂度

我读到这里从一个std ::双端队列具有以下特点公认的答案

1- Random access - constant O(1)
2- Insertion or removal of elements at the end or beginning - amortized constant O(1)
3- Insertion or removal of elements - linear O(n)
Run Code Online (Sandbox Code Playgroud)

我的问题是关于第2点.双端队列如何在结束或开始时进行摊销?

我知道a std::vector在最后插入时具有摊销的常数时间复杂度.这是因为矢量是连续的并且是动态阵列.因此,当最后一个内存耗尽push_back时,它将分配一个全新的内存块,将现有项目从旧位置复制到新位置,然后从旧位置删除项目.我理解的这个操作是摊销不变的.这如何适用于双端队列?如何在双端队列的顶部和底部插入是否可以摊销.我的印象是它应该是常数O(1).我知道一个双端队列由内存块组成.

c++ deque c++03

10
推荐指数
1
解决办法
2075
查看次数

二维数组的自动大小扣除

我正在尝试制作一个固定大小的矩阵类。目的是让它继承或利用 a std::arrayof std::array

template <size_t Rows, size_t Columns, class T=double>
struct Matrix : public std::array<std::array<T, Columns>, Rows> {
};
Run Code Online (Sandbox Code Playgroud)

我想提供一个可以自动推断大小的初始化程序,就像std::arrayC++17(我正在使用)中的 can一样。我也可以使用函数来进行Matrix而不是使用类模板参数推导。

// What I want, but does not work:
Matrix matrix {{1., 2.},
               {3., 4.}};

// Or:
auto matrix = MakeMatrix ({1., 2.},
                          {3., 4.});
Run Code Online (Sandbox Code Playgroud)

我未能使其中任何一个成为可能。相反,只有以下工作:

// Requires specifying the size and repeating `std::array`, both undesired
Matrix<2,2> mat {
    std::array{1., 2.},
    std::array{3., 4.}
};

// OR this, which requires specifying the size and …
Run Code Online (Sandbox Code Playgroud)

c++ templates matrix stdarray c++17

5
推荐指数
1
解决办法
250
查看次数

标签 统计

c++ ×2

c++03 ×1

c++17 ×1

deque ×1

matrix ×1

stdarray ×1

templates ×1