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

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];

Related

std::vector vs std::array vs normal array

I give online coding contests, where the speed of the program is everything. Over the time I have come across 3 ways to use the concept of array in C++. The questions of the contests usually require us to create a dynamic array of the given size. So its just a one time creation of the dynamic array as per the input and we don't resize the array again.
std::vector
Vectors look the most fancy and everyone tends to love them. But few days back one of the question gave me the TIME_LIMIT_EXCEEDED error when doing it with vectors.When I implemented the same logic with normal arrays, the program was submitted successfully.On researching, I found out that using the push_back() function takes a long time as compared to a normal arr[i]=x;
std::array
I don't have much knowledge about its performance. But it looks like a nicer way to handle arrays.
default arrays in C++
I do the dynamic allocation using int *arr=new int[given_size]; and then use the array normally.Passing an array as argument is not as simple as vectors but its not a big deal.
Apart from this there are also times when I have to work with 2D arrays and I am always unsure about what could be the fastest way. vector<vector<int>> is regarded slow in some forums and so is using multidimensional pointers. So I like to use a 1D array with a typedef function to handle its index but it gets complicated when I have to pass a row to a function.
Most of the answers in forums are based on what the OP was trying to do and this gives different answers. What I want to know is which is the best and long term way to use to have maximum speed/efficiency.
push_back takes a long time compared to arr[i]=x;.
Sorry but you are showing your lack of experience with vectors here, because your examples do two different things.
You are comparing something like this code
vector<int> vec; // vector created with size zero
for (...)
vec.push_back(x); // vector size increases
with this code
int arr[N];
for (...)
arr[i] = x;
The difference is that in the first case the vector has size 0 and it's size increases as you add items to it (this takes extra time), but in the second case the array starts out at it's final size. With an array this is how it must be, but with vectors you have a choice. If you know what the final size of the vector is you should code it like this
vector<int> vec(N); // vector created at size N, note use () not []
for (...)
vec[i] = x;
That is the code you should be comparing with the array code for efficiency,
You might also want to research the resize and reserve methods of a vector. Vectors (if nothing else) are much more flexible than arrays.

C++, one block of memory vs multiple

I am new to C++ and wanted to create a class for matrix and found two methods:
allocating one block of memory (which is faster as I read)
allocating multiple blocks (one for each line which is slower)
But what is better?
on the one hand I can use the second like mat[i][j] which doesn't work with the first method.
and I need to write a function that multiplies matrixes and I'm afraid that the first method will make things really hard when trying to access members
Where did you read that? 2-dimensional matrix can be represented both as a 1-dimensional array or 2-dimensional array. It's just a matter of references with 2 index. So for an element of having index row and col you can get its 1-dimensional index like this: row * matrix_width + col. So there's no impact on the speed besides calculating this index formula.

Double Vector issue in c++

So I was looking to make basically a double array but have it be dynamic via vectors. However I guess I do not fully understand this as I am doing something wrong causing my vector to be subscript out of range. Here below is my code:
std::vector<std::vector<std::string>> m_menuList;
m_menuList[0].push_back(menuType);
m_menuList[0].push_back(location);
This might be a stupid mistake or something completely wrong, it does compile correctly, just always crashes.
Thanks!
We tend to call "double vectors" 2D Vectors. A "triple vector" would then be a 3D Vector and so on...the whole lot of them can be called Multidimensional Vectors.
Your issue is that the outer vector is empty, so there is no m_menuList[0]. You could instead do:
m_menuList.push_back(std::vector<std::string>(1, menuType));
Which will push a vector consisting solely of menuType to the back of m_menuList.
You can also start m_menuList off with a certain number of empty vectors so that your current code would work (assuming you have some integer n):
std::vector<std::vector<std::string>> m_menuList(n);
You have to resize() your vector after initialization before accessing it using index.
std::vector<std::vector<std::string>> m_menuList;
m_menuList.resize(1);
But when you insert objects you dont need index, you can do as follows:
m_menuList.push_back(menuType);
m_menuList.push_back(location);
OR
std::vector<std::vector<std::string>> m_menuList;
m_menuList.push_back(menuType);
m_menuList.push_back(location);

searching a 2D array given the address of one element

A rather quick question concerning pointers in c++
My problem is,let's say I have a function isWon(char * sign, int i, int j). I call this method by giving
the address of an element in a 2D array
it's coordinates in a locally declared array
Is there any way of e.g. knowing the elements neighbors and getting to them?
Thanks for the help :)
If the array is a true array 2D array and not an array of pointers or something like that, then you can add/subtract to/from sign to get other elements' addresses.
For example, memory-wise the previous element in the array is at sign - 1. If you think of your 2D array as a grid, sign - 1 might not be the element in the previous "column".
You have to be careful how much you step in your array and ask yourself why you resort to such low-level dangerous mechanisms that feel out of place in C++.

C++ std::map vs dynamic array

I'm trying to make a 3 dimensional array of booleans that tells me if I previously visited a location in 3d space for a simple navigation algorithm. The array could be quite large (something along the lines of 1,000,000 x 1,000,000 x 1,000,000 or maybe larger), so I'm wondering if it would be faster to declare an array of that size and set each boolean value to false, or to make a map with a key of coordinate (x, y, z) and a value of type bool.
From what I figure, the array would take O(1) to find or modify a coordinate, and the map would take O(log n) to find or insert a value. Obviously, for accessing values, the array is faster. However, does this offset the time it takes to declare such an array?
Thanks
Even at 1 bit per bool, your array will take over 2**39 bytes. I'd suggest a set if there aren't too many elements that will be true.
You can use a class to hide the implementation details, and use a 1D set.
Have you tried calculating how much memory would be needed for an array like this? A lot!
Use std::map if ordering of the points is important, or std::unordeded_map if not. Also the unordered map gives you a constant time insertion and lookup.
I guess that some kind of search tree is probably what you're looking for (k-d tree for example).
You're going to make an array that is one exabyte, assuming that you use 8 bits per point? Wow, you have a lot of RAM!
I think you should re-think your approach.