我是第一次使用 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 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?
这篇关于如何实现二维向量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)