Pushing an array into a vector

后端 未结 8 1445
一整个雨季
一整个雨季 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条回答
  •  猫巷女王i
    2020-12-17 02:11

    You can resize vectors and then use copy.

    int A[2][3]={{1,2,3},{4,5,6}};
    std::vector< std::vector > vec;
    
    vec.resize(2);
    for (int i=0; i<2; i++)
    {
        vec[i].resize(3);
        std::copy(A[i], A[i]+3, vec[i].begin());
    }
    

    Is it practical? Definetly not.

提交回复
热议问题