简体   繁体   中英

C++ 2D vector declaration and element access from debugger

I'm currently trying to create a dimensional vector of doubles of 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.

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.

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.

Your syntax for LDB1[i][j] is correct. 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.

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. Your debugger probably has a problem displaying overloaded operator[] (this is a separate large topic).

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. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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