相关疑难解决方法(0)

为什么拼接整个列表或std :: forward_list的线性范围?

将一个范围从一个列表拼接到另一个列表可以在恒定的时间内完成,但代价是使size()线性变得复杂.

C++ 11已经改变了,在std::list需要size()恒定时间的情况下.这打破了,例如,gcc的实现,参见[C++ 0x] std :: list :: size complexity.

除了范围之外splice(),还有其他原因导致为什么 size() 不能在早期的符合C++ 03的 实现中保持恒定的时间std::list

为什么拼接整个列表或线性范围 std::forward_list

参见splice_after()案例(1)和(3).另见标准草案N3485中的23.3.4.6 forward_list操作[forwardlist.ops] .在std::forward_list甚至没有实现size().

我知道forward_list是一个单链表,但我不明白为什么人们不能splice_after()在恒定时间内完成该范围.我可能在这里遗漏了一些微不足道的东西......


编辑:好的,至少部分是我的误解,我预计4 不会留在源列表中.码:

#include <algorithm>
#include <iostream>
#include <forward_list>

using namespace std;

void dump_list(const forward_list<char>& l) {
    for(char c : l)
        cout << c << ' ';
    cout << '\n';
}

int main() …
Run Code Online (Sandbox Code Playgroud)

c++ list c++11 forward-list

9
推荐指数
1
解决办法
664
查看次数

标签 统计

c++ ×1

c++11 ×1

forward-list ×1

list ×1