我如何在int类型的2d向量中push_back数据 [英] How can I push_back data in a 2d vector of type int

查看:58
本文介绍了我如何在int类型的2d向量中push_back数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量,并且想在运行时将int数据存储到其中,是否可以通过这种方式将数据存储在2D向量中?

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<std::vector <int>> normal:
    for(i=0;i<10;i++){
        for(j=0;j<20;j++){
            normal[i].push_back(j);
    }
}

推荐答案

是的,但是您还需要推送每个子向量:

Yes, but you also need to push each of the sub-vectors:

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

这篇关于我如何在int类型的2d向量中push_back数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆