如何实现2D向量数组? [英] How to implement 2D vector array?

查看:99
本文介绍了如何实现2D向量数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用STL库中的向量类。如何添加到向量数组的特定行?

I'm using the vector class in the STL library for the first time. How should I add to a specific row of the vector array?

struct x{
     vector <vector <int> > v;
     int row;
 };

vector< int* > my ints;
int add;

如果我想添加到第一行的v的第一个指针的整数, / p>

if i wanted to add to first row of v with the first pointer of integers, could I do

myints[0]->v[myints[0]->row].push_back(add);

这个方法很好,可以创建一个向量的2 D向量 int s其中每行可能有不同的长度(即有不同的列数)?

Is this method fine to create a 2 D vector of vector ints where each row could potentially be of different length (i.e. have a different number of columns)?

推荐答案

我不确定是什么问题,因为你的示例代码有几个错误,并没有真正清楚你想要做什么。但是下面是如何添加到2D向量的特定行:

I'm not exactly sure what the problem is, as your example code has several errors and doesn't really make it clear what you're trying to do. But here's how you add to a specific row of a 2D vector:

// declare 2D vector
vector< vector<int> > myVector;
// make new row (arbitrary example)
vector<int> myRow(1,5);
myVector.push_back(myRow);
// add element to row
myVector[0].push_back(1);

这是否回答了您的问题?如果没有,你可以尝试更具体地了解你遇到的问题吗?

Does this answer your question? If not, could you try to be more specific as to what you are having trouble with?

这篇关于如何实现2D向量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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