Concatenating two std::vectors

后端 未结 25 2294
予麋鹿
予麋鹿 2020-11-22 12:00

How do I concatenate two std::vectors?

25条回答
  •  抹茶落季
    2020-11-22 12:34

    A general performance boost for concatenate is to check the size of the vectors. And merge/insert the smaller one with the larger one.

    //vector v1,v2;
    if(v1.size()>v2.size()) {
        v1.insert(v1.end(),v2.begin(),v2.end());
    } else {
        v2.insert(v2.end(),v1.begin(),v1.end());
    }
    

提交回复
热议问题