Creating a new C++ subvector?

后端 未结 4 477
遇见更好的自我
遇见更好的自我 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:55

    One of std::vector's constructor accepts a range:

    std::vector v;
    
    // Populate v.
    for (int i = 1; i <= 10; i++) v.push_back(i);   
    
    // Construct v1 from subrange in v.
    std::vector v1(v.begin() + 4, v.end() - 2);
    

提交回复
热议问题