Double Vector issue in c++ - 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);

Related

How to retrieve a value from a vector of vectors?

I am trying to make a vector that contains vectors, that contains a string. I am unsure on how I would go about retrieving said string. I've tried vector.at() but I'm not sure how to make a sort of 'multi-layered' version of it.
This is the value inside of my code:
std::vector<std::vector<std::string>> dialoguestore;
There are many ways to go about this with a 2D vector.
Option 1: at():
dialoguestore.at(index).at(index2);
Option 2: operator[]:
dialoguestore[index][index2]
If you have a multidimensional vector, use the ways you normally access a vector, but for the number of dimensions.

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

Compare vectors and add missing elements

I need a function like this:
vector a;matrix A(a.size(),vector(9));
vector b;matrix B(b.size(),vector(9));
....
vector n;matrix N(n.size(),vector(9));
for(all vectors and matrices given){
if(vector[i] not in other vector){
put missing element to vector at position i
put zero vector to B at position i
}
}
I want to give you a case example to make it a bit clearer:
a=[name,place]; A=[vector[names],vector[place]]
b=[name,religion]; B=[vector[names], vector[religion]]
c=[place,religion]; C=[vector[place],vector[religion]]
The aim is now to end up with a=b=c=[name,place,religion] and A~B~C=[vector[names],vector[place],vector[religion]]
Where the vectors in the A,B and C are still holding the original data, as well as zero vectors in the case the property was originally not there.
In each vector all the elements are different from each other (so no vec=[1,1,2] or similar)
Im sorry about that very abstract description. I hope one gets what I need, otherwise just ask :-)
Thanks already for your help!
As for the vectors at least, you could create a set, insert all items from all vectors into the set, and then copy from the set into all vectors. Then you should have all data from all vectors without duplicates.

Is it okay to use constructors to initialize a 2D Vector as a one-liner in C++?

Is it okay to initialize a 2D vector like this (here all values in a 5x4 2D vectro are initialized to 3)?
std::vector<std::vector<int> > foo(5, std::vector<int>(4, 3));
This seems to behave okay, but everywhere I look on the web people seem to recommend initializing such a vector with for loops and push_back(). I was initially afraid that all rows here would point to the same vector, but that doesn't seem to be the case. Am I missing something?
This is perfectly valid - You'll get a 2D vector ([5, 4] elements) with every element initialized to 3.
For most other cases (where you e.g. want different values in different elements) you cannot use any one-liner - and therefore need loops.
Well, the code is valid and it indeed does what you want it to do (assuming I understood your intent correctly).
However, doing it that way is generally inefficient (at least in the current version of the language/library). The above initialization creates a temporary vector and then initializes individual sub-vectors by copying the original. That can be rather inefficient. For this reason in many cases it is preferrable to construct the top-level vector by itself
std::vector<std::vector<int> > foo(5);
and then iterate over it and build its individual sub-vectors in-place by doing something like
foo[i].resize(4, 3);