Multiple indices into a 1D array - c++

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

Related

Getting Confused While Interpreting a Statement Of Multidimensional Vectors in 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.

Does an array of vectors form a 2D vector?

Does the following statements form a 2D vector?
Also mention the dimensions of the vector if it does. Is there another way of declaring a global 2D vector?
Statements:
vector<int> *adj;
adj = new vector<int>[number_of_nodes];
The above statements have been used for declaration of a global adjacency matrix for a graph.
It seems the question isn't clear to much of you. I want to declare a global adjacency list using vectors such that I can use direct addition of edges in graph like v[a].push_back(b). For that, I have to specify first dimension of vector while declaring which I don't have until the main is executed.
No, but it gives similar behaviour. So what you have created is a pointer to an array of vectors. So you will end up with this:
adj[0] = []
adj[1] = []
...
adj[number_nodes] = []
and doing a push_back is a legitimate way to add to the vectors:
adj[0].push_back(some_num) --> adj[0] = [some_num]
But this is a terrible way to approach this! Why?
You are using raw memory that you will have to manage and make sure you delete.
You cant use any of the awesome std::vector functionality on the first dimension of the matrix.
There is no good way to figure out the size of the vector unless you know about the variable number_of_nodes.
... A long list, but you get it.
You can already see that a std::vector can be used as a 1D matrix. So use 2 vectors:
std::vector<std::vector<int>> Matrix2D;
If you need an initial size in some dimension then you can do this:
std::vector<std::vector<int>> Matrix2D(number_of_nodes, std::vector<int>());
Or this:
Matrix2D.resize(number_of_nodes);
Or if that size is fixed at compile time then you could even do this:
const int number_of_nodes = 10;
std::array<std::vector<int>, number_of_nodes> Matrix2D;
Or go extra big and get a library for Matrix use like Eigen.

Vector Vector C ++

Vector Vector C ++
Hi, I do not understand the syntax of nested vectors to simulate an array, I have the following code.
vector< vector< float> > myvector (n, vector < float> (2));
But I do not quite understand how it works, especially where you specify the size of the vectors and the vectors in it, if you want to make a resize so that my vector vector has specified dimensions how can I resize internal vectors?
Something like changing vec [10] [2] to vec [10] [5] (changing the second dimension)
In addition to how to make copies with multidimensional vectors something like:
vector< int> myvector (myVectorToCopy, myVectorToCopy+myVectorToCopy.size());
But with several dimensions.
Thanks.
vector<vector<float>> means you are creating a vector which contains a vector of floats. The constructor argument means you are creating a vector of size n where each element of vector is a vector of floats having size 2.
To resize the vector<vector<float>>:
for (int i = 0; i < n; ++i)
A[i].resize(newSize);
Alternatively you could use:
A.assign(n,vector<float>(newSize));
To make a copy of multidimensional vector use constructor:
vector<vector<float>> B(A);
Vectors in C++ will resize automatically if you fill them up and try to add more to them. If you know the exact size of your vector, I would suggest switching to std::array, however you will lose the ability to resize them at runtime.
std::vector::operator[] has an overload to return a reference to the T used to create the template (in your case T is std::vector, the nested one). If you know the index in the outer vector, you could do something like:
myVec[0].resize(5);
This will resize your nested vector at position 0 to 5 elements.
Copying is much the same as accessing:
std::copy(std::begin(VecToCopy), std::end(VecToCopy), std::begin(VecToFill));
Using std::begin last might not be what you want but its just an example.
http://en.cppreference.com/w/cpp/algorithm/copy
http://en.cppreference.com/w/cpp/container/array

Cannot access element of 2d heap vector using operator[]

I have a 2d vector that needs to be allocated on the heap and am using the below line of code to declare and size it.
vector<vector<double>> *myArray = new vector<vector<double>>(x, vector<double>(y));
where x and y are the number of rows and columns respectively.
When I try to access an element of the vector using myArray[0][0] = 3.0;, I get the following error,
error: no viable overloaded '='
myArray[0][0] = 3.0;
I would appreciate any help in figuring this out.
Notes:
The number of rows and columns needs to be dynamic, hence myArray is on the heap.
The array needs to be resizable, which is why I am using std::vector.
I understand that I can create a vector of vectors (number of rows) and then in a for-loop resize each row element to the required number of columns. What I do not understand is why the above code does not work since as far as I know it should perform the same function.
For some strange invalid reason, you are using a pointer to a vector. Since operator[] works with pointers, when you do
myArray[0][0] = 3.0;
you are actually accessing a vector<double>, not a double, because myArray[0] gets you a vector<vector<double>>.
The obvious fix is not to use a pointer in the first place:
vector<vector<double>> myArray(x, vector<double>(y));
must be:
(*dataArray)[0][0] = 3.0

How to use a "vector of vector"?

I already searched on the web for it but I didn't get satisfying results.
I want to create something like
vector< vector<int*> > test_vector;
How do i fill this vector of vector? How to access it's members? Maybe someone knows some nice tutorials on the web?
kind regards
mikey
Just remember that each element of test_vector is of type vector<int*>. You would fill test_vector by filling each element vector.
You can access this just like any multi-dimensional array. See:
int *p = test_vector[0][0];
Or:
int *p = test_vector.at(0).at(0);
A question similar to yours was posted at DreamInCode: http://www.dreamincode.net/forums/topic/37527-vector-of-vectors/
You fill a vector of vectors by putting vectors in it.
You access its members the same way you would any other vector.
PS
If you want to use some kind of a matrix, I would prefer to use only one dimensional vector and map the access (because of performance).
For example Matrix M with m rows and n columns: you can map call
M[i][j] = x to M[i*n+j] = x.