如何实现二维向量数组?

How to implement 2D vector array?(如何实现二维向量数组?)
本文介绍了如何实现二维向量数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是第一次使用 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?

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)