Getting Confused While Interpreting a Statement Of Multidimensional Vectors in c++ - c++

I am kind of new to Working With 2D Vectors With C++ , and Often times I get confused while I am
Working With 2D Vectors in C++ . I was going through someone's code and I am getting confused while interpreting this line of code :
vector<vector<int>> dp(n, vector<int>(m));
Here , m and n are number of rows and columns of a Grid .
Can Somebody please explain to me in detail , what does this statement mean ?

This line:
vector<vector<int>> dp(n, vector<int>(m));
is invoking a constructor of std::vector.
For dp, the first argument is the number of elements, and the second argument is the value to be inserted those many times.
The inner vector is not specifying the argument value, which will insert default values (0 in the case of int).
So in this case, you are creating a vector with n rows, where each row has m elements in it, and each of the elements is 0.

http://www.cplusplus.com/reference/vector/vector/vector/
One of the constructors of Vector takes param 1 as the number of elements and 2 as the value of those elements. In this case, you are creating a vector named dp... it will have n elements... and each of those elements will be initialized as a new vector of size m elements.
The n and m values only really pertain to the initial size... Vector will automatically resize itself as new elements are added or as you explicitly tell it to if your expect significant size changes to be coming

You can break it down if it helps
vector<vector<int>> dp(n, vector<int>(m));
is effectively the same as
vector<int> row(m);
vector<vector<int>> dp(n, row);
All the first version does is eliminate the row variable.

Related

How to assign a specific number to all elements of matrix in vector<vector<int>> matrix_name without using for loop stuff?

like we do this in array thing to assign a specific num to all elements of array-
vector<int> arr(n,-1);
So ya,above i mention how to do it in array but what about vector<vector> matrix_name?
Just as you can initialize a vector with a number of integers, given a default integer value, you can also initialize a vector with a number of other vectors, given a default value for that other vector. That way you can easily create a multidimensional vector.
Example:
int nr_of_rows = 30;
int nr_of_columns = 40;
std::vector matrix(nr_of_rows , std::vector(nr_of_columns , -1));
This creates a vector of 30 vectors of 40 integers with a default value of -1.
matrix[2][17] gives you vector with index 17 inside vector with index 2 of your matrix.

comparison of two arrays and removing duplicates

Hi Here are my Two Arrays.
A[]={1,2,3,4,5}
B[]={3,4,5}
Expected Output:
C[]={1,2}
Can someone try to explain the solution in c++
Please find the code that i tried.We had some problem uploading the code.Please find the algorithm we tried
1) Taken Two Arrays A and B
2) Array A containing m elements and B contains n elements.m>n
3) Took the inputs for Array A and Array B from Standard Input
4) Comparing the elements in both the Arrays element wise using two for loop
where each element of array A compared with each element of Array B and if
not equal push the element into new array.
But we faced two problems here once in case the first element of Array A not equal to first element of B it will be taken as not a duplicate.But that element will be equal to the last element of array B in that case my code fails.
Next is that we are able to get the elements which are not duplicate from Array A into C but if want to get the elements which are not duplicated from array B into C.Do we need to implement for loop again.?
If you use std::vector, so can use std::find for find a element of B in A.
for(int i = 0; i<B.size(); i++)
std::find(A.begin() ,A.end(), B[i])

use memcpy and begin() to copy a sub-array of a multidimensional array

I am trying to use memcy to copy an sub-array of an updated array A into an array B. Both A and B array have the same number of dimensions. The data going into array A is in the order of A.get_size(3) (Let's call dimension N here). Each update N will increase by 3(That means 3 set of data is added queueing in terms of dimension N). I need B to obtain the updated data (sub-array)only and the next copy will replace the previous 3. So I guess copy array A starting from (N-2) into B could do the job.
//initializing a new array B to obtain sub-array from A
array B(A.get_size(0), A.get_size(1), A.get_size(2), 3, A.get_size(4));
//Get the forth dimension N
N=A.get_size(3);
//copy the sub-array of A (from N-2 to N) into B(from 1 to 3 in dimension N)
memcpy(B.begin()+A.get_size(0)*A.get_size(1)*A.get_size(2)*(1)*A.get_size(4), A.begin()+A.get_size(0)*A.get_size(1)*A.get_size(2)*(N-2)*A.get_size(4), sizeof(T)*(A.get_size(0)*A.get_size(1)*A.get_size(2)*A.get_size(4)));
However, this could copy the number of elements but not the data value inside. Does it mean it gets memory leak? How should I make changes? Thanks!
Edited:
Thanks to the comments, I tried to use std::copy instead of memcpy, but this does not work in this case because the array A contain complex float. It gives this error when compiling,
error: no matching function for call to ‘copy(std::complex<float>*, const std::complex<float>*, long unsigned int)’
Anymore ideas except from using std::copy? Thanks!

Inserting elements into 2D vector

so I'm creating a class that implements an adjacency list. Currently in my class definition I initialized two vectors:
vector<vector<int>> adjList;
vector<int> neighbors;
and I declared two functions that I plan to use to make it:
bool constructAdjList();
bool insertIntoAdjList(int, int);
It's getting difficult wrapping my head around 2D vectors. I understand that it is essentially a vector of vectors, but I'm confused about how to insert a new value into one of the "subvectors". For example, I am able to create an adjacency list in createAdjList that is empty with the following loop:
for (int i = 0; i < numOfValues; i++){
neighbors.push_back(0);
adjList.push_back(neighbors);
neighbors.clear();
}
But how can I say, push_back the value 5 to the 4th vector in adjList, which would be represented in my insertIntoAdjList function as
insertIntoAdjList(4, 5);
I know I can access a specific value in a 2D vector by saying adjList[4][1], but how can I push one onto it?
Thanks!
To push on the vector that is an element of another vector, you simply do this
adjList[x].push_back();
If initially you do not have any values in the vector -
You can push values into one vector and then push this vector into the 2D vector.
For example:
vector< vector<int> > vt1;
vector<int> vt2;
vt2.push_back(value);
vt1.push_back(vt2);
If your vector is already populated then -
vt1[index].push_back(value);
A couple of notes here.
Your loop can be significantly shortened just be using the constructors of your two members:
vector<int> neighbors(1, 0); // set to length 1, value is zero
vector<vector<int>> adjList(numOfValues,neighbors); // "outer" vector is numOfValues long
. // each row is a *COPY* of neighbor
If you can't do this at construction time (maybe numOfValues isn't known yet), then there's still a better loop phrasing we can use:
// neighbors object can be reused
neighbors.clear(0);
neighbors.push_back(0);
adjList.reserve(numOfValues); // reserving memory ahead of time will prevent allocations
for (int i = 0; i < numOfValues; i++){
adjList.push_back(neighbors); // push_back is by *COPY*
}
In your example, by using clear and push_back to essentially build the same vector every loop iteration, you are risking an allocation and deallocation each iteration. In practice, most implementations won't do this, but if we can both shorten and potentially make things more efficient, we may as well.
Lastly, if the number of neighbors is relatively small and similar row to row (for instance a finite elements code with tetrahedral elements, where each element connects to ~5 others), then as others have suggested you may be better off with a different structure than vector-of-vector. For instance, a single vector that is logically organized such that a new "row" begins every N elements.

Multiple indices into a 1D array

I was trying to implement strongly connected algorithms from different sources. I found one that confuses me at http://www.oneous.com/Tutorial-Content.php?id=18
The reason for my confusion is this line:
if (used[v[s][i]] == 0)
DFS(v[s][i]);
We have declared v as a one-dimensional vector, but here it is used as a 2-dimensional vector. I think this is an error but can't figure out how to fix it. Please help me solve this problem.
v is declared as:
vector<int> v[1005];
i.e. an array of vectors. So the first index accesses into the array, the second accesses into the vector.
v is an array of vectors. So v[1] is a vector<int> at position 1, and v[1][2] is an integer at vector<int> position 2.
Vector class allows that because it overrides [] operator