Pushing an array into a vector

后端 未结 8 1411
一整个雨季
一整个雨季 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:22

    You can use vector::assign (pointers to array elements are valid iterators):

    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
    std::vector<std::vector<int> > v(2);
    for (size_t i = 0; i < 2; ++i)
       v[i].assign(a[i], a[i] + 3);
    
    0 讨论(0)
  • 2020-12-17 02:26

    Hm... I can produce a partial answer but not a full one.

    int elementCount = 6; // I wonder if this can be done somehow with sizeof(A) * sizeof(A[0])
    int* end = A + elementCount;
    for(int* current = A; current < end; ++current) {
        myvector.pushback(*current);
    }
    
    0 讨论(0)
提交回复
热议问题