Best way to split a vector into two smaller arrays?

前端 未结 4 1725
一个人的身影
一个人的身影 2021-02-01 16:01

What I\'m trying to do:

I am trying to split a vector into two separate arrays. The current int vector contains an element per line in a text file. Th

4条回答
  •  野的像风
    2021-02-01 16:28

    If you can't use code from Xeo answer due to strict compiler rules or you want more generic way, try std::advance:

    #include 
    #include 
    
    size_t middle = input.size()/2;
    std::vector::const_iterator middleIter(input.cbegin());
    std::advance(middleIter, middle);
    
    std::vector leftHalf(input.begin(), middleIter);
    std::vector rightHalf(middleIter, input.end());
    

提交回复
热议问题