Pushing an array into a vector

后端 未结 8 1414
一整个雨季
一整个雨季 2020-12-17 01:26

I\'ve a 2d array, say A[2][3]={{1,2,3},{4,5,6}}; and I want to push it into a 2D vector(vector of vectors). I know you can use two for loops to pus

8条回答
  •  轮回少年
    2020-12-17 02:15

    If you want to push the data into vector of vectors, you have to write something like this:

    vector inner;
    vector< vector >outer;
    
    ...
    outer.pushback(inner);
    

    I think there is no way to do it in a single loop.

    If you want to use just one vector (something similar like you written), then you can do it in a single loop:

    int A[2][3]={{1,2,3},{4,5,6}};
    int* p = A[0];
    std::vector inner;
    std::vector< std::vector >outer;
    
    for(int i = 0; i < 6; ++i)
    {
        inner.push_back(*p++);
    }
    

提交回复
热议问题