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

前端 未结 6 868
借酒劲吻你
借酒劲吻你 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
    慢半拍i (楼主)
    2021-02-02 03:23

    You cannot directly assign to [i] without allocating the outer and inner vectors first. One solution to this would be to create the inner vectors inside your for loop, then once those are populated, push_back to the outer vector.

    std::vector> normal;
    for(i=0;i<10;i++)
    {
        std::vector temp;
        for(j=0;j<20;j++)
        {
            temp.push_back(j);
        }
        normal.push_back(temp);
    }
    

提交回复
热议问题