vector emplace_back usage (C++11) of containing another vector (partial vector)

2.6k views Asked by At

Has anyone known related reference for such emplace_back usage (C++11)?

void func(const vector <int>  &A){
    vector <vector <int> > B;
    B.emplace_back(A.cbegin() , A.cend());  // emplace vector A directly
}
2

There are 2 answers

1
herohuyongtao On

You'd better searched them on Google or cppreference.com before asking for any further reference.

1
Daniel Frey On

I'm not sure what your real question is, but the code you have shown inserts a new vector in-place inside B. The new vector is initialized by calling its constructor (4) which takes the two arguments A.cbegin() , A.cend().

You can think of it as an optimized version of

B.push_back(std::vector<int>(A.cbegin() , A.cend()));