Compare vectors and add missing elements - c++

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.

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.

Accessing elements of a vector in a map of vectors

I want to create a map of vectors. I want the vector to be a private member variable however so when I need to increase the size of the vector for a particular key in the map it does it for all other keys in the map also(would that work?). This will be a map of vectors(of ints) where the keys are strings. My question is how to access a particular element in the vector to change is value in C++. Something along the lines of map_name['word'].[3] = 2 if i wanted to set the third value of the vector of "word" to 2.
enter image description here
enter image description here
Im still having trouble figuring out how to make it so the size of each vector for all the keys in the maps is modifiable so i can increase the size of each vector at any point along the program. This is b/c the vector size is unknown at runtime and iterating through each element in the map to change the vector size will take too long.
The pattern is recursive.
That is, when you do:
expression[key] = value;
your expression doesn't have to just be a variable name; it can be a more complex expression, such as map_name["word"].
So:
map_name["word"][3] = 2;
Regarding the first question, yes it is possible as mentioned in one of the comments, you can make your imaginary class to do that.
And in the second question, you'll have to access an element of a vector which is an element of a map like this:
map1["abc"][1] = 2
The '.' you added was unnecessary because you're accessing an element inside another element, just like a 2D array

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

C++ check if element exists in vector

I'm working with a vector and need to check if there exists an element at a specific spot in the vector, like myvec[7]
I need to work outside of a loop and be able to perform this check for any given point in the vector. What would be the most efficient way to do this with a vector?
This is the check you are looking for: (myvec.size() >= 8). In C++ there are no blank elements in a vector - i.e. the vector elements are with consecutive indices.
There are two ways of doing this.
Next code examples will assume we want to do something with element v[n] in vector v.
Way 1:
if(n<v.size())
//do something with v[n]
else
//do something else
Way 2:
//do something using v.at(n) instead of v[n]
This will raise an exception if you try to access element that isn't in vector.
Which one to use? Depends on the case.
Can you work if element isn't in the vector? Use first method.
Having this element is crucial for your code. Use second method, let STL check its presence for you and don't forget to catch an exception.

What's the best way to read a matrix of unknown columns from standard input?

I know only the number of rows r in a matrix.
How do I read it into a multi-dimensional array arr[MAX][MAX]?
I thought of reading all the elements into a single array, count the no. of elements and then adding them to arr in groups of count/r. Is there a simpler way?
You could use the fact that everything may as well go into contiguous memory so just keep pushing it at the end of std::vector<double>. At the end you know its length, and given that you know r, you now also know the number of columns.
If you really have nothing but the number of rows and a list of data values, just read the whole thing into a vector, and then divide the size of the vector by the number of rows to get the number of columns. You should, however, also know whether the data is stored row-wise or column-wise. On this depends how to index the vector (I would keep the data in the vector and access it through index calculation, most probably encapsulated in a nice little class).