Creating a new C++ subvector?

后端 未结 4 470
遇见更好的自我
遇见更好的自我 2021-01-07 19:08

Say I have a vector with values [1,2,3,4,5,6,7,8,9,10]. I want to create a new vector that refers to, for example, [5,6,7,8]. I imagine this is just a matter of creating a v

4条回答
  •  长发绾君心
    2021-01-07 19:50

    You don't have to use push_back if you don't want to, you can use std::copy:

    std::vector subvector;
    copy ( v1.begin() + 4, v1.begin() + 8, std::back_inserter(subvector) );
    

提交回复
热议问题