How to implement 2D vector array?(如何实现二维向量数组?)
问题描述
我是第一次使用 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 的第一行,我可以这样做
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);
此方法是否适合创建向量 int
的二维向量,其中每行可能具有不同的长度(即具有不同的列数)?
Is this method fine to create a 2 D vector of vector int
s 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?
这篇关于如何实现二维向量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何实现二维向量数组?


- 静态初始化顺序失败 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- 近似搜索的工作原理 2021-01-01