相关疑难解决方法(0)

如何使用std :: forward_list在恒定时间内进行范围拼接?

我想拼接范围[first, last],包括两个端点.我之前 first之前都有元素的迭代器last.我能做到splice_after()但只能在线性时间内完成.

我相信这种拼接可以在恒定的时间内完成.我怎么能这样做std::forward_list

如果问题不明确,这里显示我的问题的示例代码:

实时工作空间代码

#include <algorithm>
#include <forward_list>
#include <iostream>
#include <iterator>
using namespace std;

int main() {   
    forward_list<char> trg{'a','b','c'};
    forward_list<char> src{'1','2','3','4'};

    auto before_first = src.begin();
    auto last = find(src.begin(), src.end(), '4');
    cout << "before_first = " << *before_first << ", last = " << *last << "\n";

    // trg.splice(trg.begin(), src, before_first, last); // no such splice
    auto end = last;
    ++end; // Ouch! …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 forward-list

6
推荐指数
1
解决办法
418
查看次数

标签 统计

c++ ×1

c++11 ×1

forward-list ×1