Best way to split a vector into two smaller arrays?

前端 未结 4 1776
一个人的身影
一个人的身影 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:19

    If you only need a reference to the numbers without manipulating them, then you can do:

    int *array_1 = &lines[0];
    int *array_2 = &lines[lines.size() / 2];
    

    array_1 and array_2 are, actually, pointers to the start and middle of the vector. This works since STL guarantees that vectors store their elements within a continuous memory. Note that referring to lines.begin() can't be used for this.

提交回复
热议问题