I'm making socoban game in C++ now. I'd like to know how can I make a pointer array which can contains multidimensional vectors.
For example,
I created some 2-demension vectors
vector<vector<char> > vector1;
vector<vector<char> > vector2;
I want to make a pointer
For example (pointer)[0] = &vector1
like this
Please tell me how.
Related
so I have the following
vector<vector< tuple<string, double>*>*>* graph;
a 2d vector, with a tuple of string and double.
I want to initialize the graph(2d vector) with a certain size and
a new vector< tuple<string, double>*>, in each of the element of the big (outside)vector
and I used the following line
graph = new vector<vector<tuple<string, double>*>*>(67, new vector< tuple<string,double>*>());
This thing works but when I tried to free it I found out that all new vectors I created are of the
same vector.
meaning, all the elements point to the same vector. I get why this is happening but
is there a way of initialize all the vectors without having to do the for loop, ie
for(int i....)
graph->push_back(new vector< tuple<string,double>*>);
Problem summary:
In the line
graph = new vector<vector<tuple<string, double>*>*>(67, new vector< tuple<string,double>*>());
constructor 3 of std::vector is used (reference)
This will evaluate new vector<tuple<string,double>*>() once, then create a vector with 67 copies of this pointer.
Solution:
Don't use pointers unless you have a really good reason to do so. Use
vector<vector<tuple<string, double>>> graph;
then you could simply do
graph.resize(67);
to insert 67 default constructed values. No pointers needed.
Maybe you are used to languages where new is frequently used, but you shouldn't do that in C++. std::vector and std::string are fairly small objects that manage an underlying dynamic array. Creating pointers to them is usually not what you want and might also decrease performance.
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 ++
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
in c# I can do this:
private double[,] elevations;
How to do the same in c++ without specifying the size or elements of array on this line?
You cannot do that for arrays in C++.
But, if you want dynamic size, go for std::vector.
And from your question, I see that you are interested in a 2D array. Hence, use a nested std::vector, like this:
vector< vector< double > > elevations;
Basic Problem Can you please help me understand how to use a vector of a vector. Take for example vector< vector<int> > help. I do not understand if it is a vector of ints who each are a vector of ints or if it is a vector of a vector of ints? I also don't understand how to utilize it.
Example Code
vector< vector<int> > test[500];
test[0].emplace_back(1);
cout << test[0][0];
test[50].emplace_back(4);
cout << " " <<test[50][0];
-console-
1 50 //this is not what happens btw, but it is the desired results
Disclaimer I have spent the better part of a morning testing and googling this. Please help :) I did my hw. I can't find any documentation of vectors of a vector. Also I have all the correct libraries and I am using namespace std. I am a noob and i understand that namespaces are bad practice, but its very convient for me right now.
Basically what I want is a set size of a vector filled with each pt being a vector of int. I would rather not go the way of a separate class. Is a vector of a vector of int, the right thing to be looking into?
Thank you :)
This is a vector of int:
std::vector<int> v;
this is a vector of vectors of int:
std::vector<std::vector<int>> v2;
this is an array of vectors of vectors of ints, which is what you have:
std::vector<std::vector<int>> test[500];
each element of that array is an std::vector<std::vector<int>>. So test[0] is one of those.
If you want a vector of 500 default constructed vectors of int, you need
std::vector<std::vector<int>> test(500);
test is an array of 500 vectors of vectors of int. The second line of your example should not even compile here, as you are calling std::vector< std::vector<int> >::emplace_back(), which expects an argument compatible with std::vector<int>, and you have provided an int. To clarify:
test is a std::vector< std::vector<int> >[500].
test[0] is a std::vector< std::vector<int> >.
test[0][0] is a std::vector<int>.
test[0][0][0] is an int.
(Pedantic C++ developers will note that the latter three are actually references, but I'm omitting that from the type for clarity.)
A vector is just a resizable array.
To declare a vector of int (an array of int), just do:
std::vector<int> vec;
To declare a array in which individual elements are vectors, you do:
std::vector< std::vector<int> > vecarr;
To set the initial size of the vector, you do:
std::vector<int> vec(500); not std::vector<int> vec[500], because this creates an array of 500 std::vectors. Similarly, std::vector< std::vector<int> > vec[500]; creates a array of 500 vector of vectors.
To skip writing std:: you can say using namespace std before all this to tell that you're using the std namespace.
What you have there is an array of vector-of-vector which I believe since you're accessing the data with two indexes is not what you wanted.
I believe you may have just typo-ed your constructor initialization:
vector< vector<int> > test(500); // Note () instead of [] here.
This creates a vector-of-vectors, with 500 inner vectors pre-created for you. Then the rest of your code should just work!