visual studio - How to perform fast merging c++ list with itself -


lets have list containing integers, need merge : example have {1, 2, 3} after procedure want list {1, 2, 3, 1, 2, 3} these versions slow when list size big enough (10^6)

    list< int > l;    //l got integers here              list< int > l_copy = l;      while (l_copy.size() > 0)     {         l.push_back(l_copy.front());         l_copy.pop_front();     }      //another version still slow think     size_t size = l.size();     (list<int>::iterator = l.begin(); size--; ++it)     {         l.push_back(*it);     } 

is there alternatives doing faster? thanks

you can use std::list::splice this:

list<int> l; list<int> l_copy = l;     l.splice (l.end(), l_copy); 

that version of splice guaranteed work in constant time standard (§23.3.5.5/6 in n4296). works pointing end of first list @ beginning of other list. there version of splice uses iterator ranges o(n), isn't needed here. copy still take time large list, that's unavoidable.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -