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

Ali*_*Ali 6 c++ c++11 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! splice has to find last again although I already had it  :(
    trg.splice_after(trg.begin(), src, before_first, end);

    cout << "Target after splice:\n";
    copy(trg.begin(), trg.end(), ostream_iterator<char>(cout," "));

    cout << "\nSource after splice:\n";
    copy(src.begin(), src.end(), ostream_iterator<char>(cout," "));

    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

before_first = 1, last = 4
Target after splice:
a 2 3 4 b c
Source after splice:
1 
Run Code Online (Sandbox Code Playgroud)

How*_*ant 5

规范forward_list(first, last)应该拼接范围,并且很遗憾没有办法在O(1)时间内执行此操作,因为需要访问权限last-1才能执行此操作,并且获取访问权限的唯一方法last-1是向前迭代first.

如果规范是拼接范围(first, last],则可以进行O(1)拼接.我知道目前的forward_list规格无法实现这一目标.

我认为这是一个缺陷.但是我已经尝试过但未能修复它:

http://cplusplus.github.com/LWG/lwg-defects.html#897

然而,问题在过去已被逆转,特别是当投诉来自非委员会成员,例如您自己.提交投诉的方式是打开一个新问题,在适当时引用任何旧问题或相关问题.打开问题的说明在这里.

PS:问题上的+1.