Desidero unire l'intervallo [first, last]
, con entrambi gli endpoint inclusi. Ho degli iteratori per l'elemento prima dellofirst
e last
. Potrei farlo con splice_after()
ma solo in tempo lineare.Come eseguire la giunzione in serie in tempo costante con std :: forward_list?
Credo che questa giuntura possa essere eseguita in tempo costante. Come posso farlo con std::forward_list
?
Se la questione non è chiara, qui come è un codice di esempio che mostra il mio problema:
Codice in 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;
}
uscita:
before_first = 1, last = 4
Target after splice:
a 2 3 4 b c
Source after splice:
1
gcc libstdC++ fa questo in tempo costante, ma visual C++ no. ([why] (http://msdn.microsoft.com/en-us/library/vstudio/ee373562%28v=vs.110%29.aspx): _Se la terza funzione membro inserisce N elementi, e & Right! = this , un oggetto di iteratore di classe viene incrementato N volte_) – neam
@tim Dove lo fai? Si prega di dare il link. – Ali
[qui] (http://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a00484.html#a90ae2ddea9cebf2b29f7399683dc3e20) (scusami mi sono dimenticato di darti il link) – neam