lundi 27 février 2017

Efficient concatenation of STL list to an existing non-empty vector

How to perform an efficient concatenation of a STL list to an existing non-vector?[C++14] Following is the code which I am trying to do it efficiently. Though it will work, I believe their will be a much better way of concatenating.

Vector V does has some elements prior to described merge operation.

Size(V) > Size(L)

If this information helps anymore.

void merge(std::list<int>& L, std::vector<int>& V) {
    for (auto& x : L) V.push_back(x);
    std::sort(V.begin(), V.end());
}

Aucun commentaire:

Enregistrer un commentaire