如何使用std :: forward_list在常量时间范围内进行拼接? [英] How to do range splice in constant time with std::forward_list?

查看:214
本文介绍了如何使用std :: forward_list在常量时间范围内进行拼接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 [first,last] 与两个端点(包括端点)进行拼接。我有之前 第一最后的元素的迭代器。我可以用 splice_after()但只在线性时间。

/ code>?



如果问题不清楚,这里是显示我的问题的示例代码:



实时工作空间上的代码

  #include< algorithm> 
#include< forward_list>
#include< iostream>
#include 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<< \\\
;

// trg.splice(trg.begin(),src,before_first,last); // no such splice
auto end = last;
++ end; //哎哟! splice已经找到了最后一次,虽然我已经有了:
trg.splice_after(trg.begin(),src,before_first,end);

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

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

cout<<< endl;
}

输出:

  before_first = 1,last = 4 
拼接后目标:
a 2 3 4 bc
源拼接后:
1


解决方案

规范 forward_list 说,范围(第一个,最后)应该拼接,不幸的是没有办法这在O(1)的时间,因为一个需要访问 last-1 这样做,以及访问 last-1 是从开始向前迭代



如果规格是拼接范围(first,last] ,那么O(1)拼接将是可能的。我知道没有办法实现这个与当前的 forward_list 规范。



我认为这是一个缺陷。但我已尝试并无法解决问题:



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



然而,过去的问题已经扭转,特别是当投诉来自非委员会成员,如你自己。提出投诉的方式是开启新问题,在适当情况下引用任何旧的或相关问题。有关打开问题的说明,请点击此处



PS:+1的问题。


I want to splice the range [first, last], with both endpoints inclusive. I have iterators to the element before first and to last. I could do it with splice_after() but only in linear time.

I belive this splice can be done in constant time. How can I do it with std::forward_list?

If the question is not clear, here as is an example code showing my problem:

Code on Live Work Space

#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;
}

Output:

before_first = 1, last = 4
Target after splice:
a 2 3 4 b c
Source after splice:
1 

解决方案

The specification of forward_list says that the range (first, last) should be spliced, and there is unfortunately no way to do this in O(1) time because one needs access to last-1 to do that, and the only way to get access to last-1 is to iterate forward from first.

Had the spec been to splice the range (first, last], then an O(1) splice would be possible. I know of no way to achieve this with the current forward_list spec.

I think it is a defect. However I've already tried and failed to fix it:

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

However issues have been reversed in the past, especially when complaints come in from non-committee members such as yourself. The way to file a complaint is to open a new issue, referencing any old or related issues if appropriate. The instructions for opening an issue are here.

PS: +1 on the question.

这篇关于如何使用std :: forward_list在常量时间范围内进行拼接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆