Search code examples
optimizationboostcoding-styleiteratorstl-algorithm

what is stl-style for merge two containers while alternating the elements?


What is an elegant way to do the following in STL-style rather then for(;;):

Given p={1,2,3} and q={7,8,9}, i'd like to merge this to be pq={1,7,2,8,3,9}. one application is creating pq vector for integrating out of position (q) and momentum (p):

for(size_t i=0; i<p.size();++i) {
 pq.push_back(p[i]);
 pq.push_back(q[i]);
}

it's not elegant, it's not stl. it works but this question is about learning stl-style correctly rather then getting the job done so it's different from https://stackoverflow.com/questions/10746197/how-to-fit-elements-of-two-vectors-alternately-in-c (please comment before closing it so i can rephrase it)

the solution that i'm looking for should use some stl-algorithms and iterator manipulation. boost is good too.


Solution

  • I don't know of an existing algorithm that's really suited to this task. The obvious alternative is to write roughly the code above, but as a generic algorithm:

    template <class InIter1, class InIter2, class OutIter>
    OutIter unsorted_merge(InIter1 b1, Inter1 e1, inIter2 b2, OutIter r) { 
        while (b1 != e1) {
            *r = *b1; ++r; ++b1;
            *r = *b2; ++r; ++b2;
        }
        return r;
    };
    

    Even though that code may not be particularly elegant or beautiful, the rest of the code can be:

    unsorted_merge(p.begin(), p.end(), q.begin(), std::back_inserter(pq));