简体   繁体   English

C ++ 2D矢量声明和调试器中的元素访问

[英]C++ 2D vector declaration and element access from debugger

I'm currently trying to create a dimensional vector of doubles of 256*256. 我目前正在尝试创建256 * 256的双精度尺寸向量。 I read the documentation and I think I'm declaring it well. 我阅读了文档,并且声明很好。

int rows1 = 256;
int cols1 = 256;

vector<vector<double>> LBP1(rows1, vector<double>(cols1));

The problem is that when i go debugging it has only 256 of size. 问题是,当我去调试时,它只有256个大小。

And when i access their elements in a cycle like: 当我像这样循环访问它们的元素时:

LBP1[i][j] 

The debug gives me: "no operator "[]" matches these operands". 调试给了我:“没有运算符“ []”匹配这些操作数“。 I already solved this problem with vector._Myfunction for one-dimensional cases but here I'm struggling a little bit with the solution. 对于一维情况,我已经使用vector._Myfunction解决了这个问题,但是在这里我在解决方案上有些挣扎。

If someone could explain me why this happens i would be very thankful. 如果有人能解释我为什么会这样,我将非常感激。

The size of the first vector will be 256, it contains 256 other vectors all of size 256. 第一个向量的大小为256,其中包含256个其他向量,大小均为256。

Your syntax for LDB1[i][j] is correct. 您对LDB1[i][j]语法正确。 Most likely your debugger is failing to handle it correctly. 调试器很可能无法正确处理它。 Check whether it works in code and that will confirm. 检查它是否在代码中起作用,然后确认。

You can try using the at method in your debugger instead. 您可以尝试在调试器中使用at方法。

http://www.cplusplus.com/reference/vector/vector/at/ http://www.cplusplus.com/reference/vector/vector/at/

Your declaration is correct. 您的声明是正确的。 LBP1.size()==256 because this is the number of elements in the outer vector. LBP1.size()==256因为这是外部向量中的元素数。 Your debugger probably has a problem displaying overloaded operator[] (this is a separate large topic). 您的调试器可能会出现显示重载operator []的问题(这是一个单独的大主题)。

First of all, you are not declaring 2-dimensional vector. 首先,您不是在声明二维向量。 You are declaring a vector of vectors, and it is usually not very syntaxically welcome. 您在声明向量的向量,通常在语法上不是很受欢迎。

If you need to perform a matrix algebra on your 2D structure, you should really use already available matrix clasess. 如果需要在2D结构上执行矩阵代数,则应真正使用已经可用的矩阵分类。 And if you simply need a structure to represent 2-D layout (a 2D coordinate map, for instance) it is much better to use a single vector and access it in a quasi-2D style, like following: 而且,如果您只需要一种结构来表示2D布局(例如2D坐标图),则最好使用单个矢量并以准2D样式访问它,如下所示:

std::vector<int> vec(n_columns * n_rows);

int i = vec[x + y * n_rows];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM