How can I push_back data in a 2d vector of type int

前端 未结 6 871
借酒劲吻你
借酒劲吻你 2021-02-02 03:03

I have a vector and want to store int data in to it at run time can I store the data in a 2D vector in this manner ?

std::vector> n         


        
6条回答
  •  梦毁少年i
    2021-02-02 03:43

    Allocate n empty vectors, that is, empty vector for each index. Then push_back() can be applied.

    int main()
    {
        int n = 10;
        std::vector> normal;
        normal.resize(n);   //Allocating 'n' empty vectors
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 20; j++)
            {
                 normal[i].push_back(j);
            }
        }
        return 0;
    }
    

提交回复
热议问题