array declaration in c++ without specifying the size - c++

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;

Related

How to use pointer array which contains multidimensional vector

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.

Initialising array from a variable (C++)

Im currently doing a programming project, and I've come across a problem while compiling... I have this code made in C++:
scanf("%d %d", &number, &shares);
vector<int> graph[number];
I read the variable number, and use it to initialise the size of the array. But this produces a initialising problem,
variable length array of non-POD element type vector<int>
Anyone knows what I can do to solve this?
You are attempting to define an array of vectors, but arrays cannot have runtime determined sizes. Presumably, you want to declare a vector of a certain type, which you can do like this:
vector<int> graph(number); // vector of size = number
Edit: After your comments, it seems that what you're after is a vector of vectors:
vector<vector<int>> graph(number);
Check this related posts, as they have this same problem as yours:
c++: Error: variable length array of non-POD element type
Variable length array of non-POD element type 'string' (aka 'basic_string<char>') c++
You're trying to create an array of vectors, which is not a good thing if you don't do this through dynamic allocation. If you want an array of vectors, do this:
vector<int> * graph = new vector<int>[number];
...
delete[] graph;
If you want to have a vector of size number, then do this:
vector<int> graph(number);

How to create variable name with integer appended to the end?

I want to create a for loop that will fill a bunch of arrays with data in c++. Now to save space and in the future once more arrays are added which they will, I have the for loop. Each array for demonstration purposes is called Array# (# being a number) The point of the for loop would be to set a constant with maximum arrays, then cycle through each array filling by appending i to the end of the Array name.
For example in pseudo code:
for (i = 1; i < numberofarrays; i++)
{ fill (Array & i) with ("Array" & i & "-default.txt")}
It is impossible to generate Variable Names by any type of code.
(Meaning it is impossible to generate dynamic variable names on Runtime or on Compiletime)
The best solution possible would be a array of arrays:
int Arrays[][];
Calling Arrays[0] would give you the first array.
If you want to determine the number of arrays during Runtime you need to use pointers!
That would look like that:
(int[])* Arrays = new (int[])[numberofarrays];
Accessing the arrays in the array would work the same!
An alternative would be using the container vector from std.
The code would the look like this:
#include<vector>
// More includes
// Optional
using namespace std;
// Somewhere in your code
vector<vector<int>> Arrays;
You still would acces the elements by using your standard array method (Arrays[15][78] e.g.)
You don't really need the name. You can use an std::vector of arrays. This will not work out of the box, see Correct way to work with vector of arrays
Another approach would be to have an std::map of arrays. You could have the name as the key, if that is what you really want. You will still have to use the same workaround as before to have an array as a value. See Character Array as a value in C++ map for example.

How to Implement a multidimensional array if the dimension is unknown at compile-time?

I want to implement a function that gets as a parameter a dimension "n" of an array of integers. This function also gets values "k_1, k_2, ..., k_n" defining the size of the array. Then this function will fill this n-dimensional array.
How do I implement this efficiently with C++?
For example for n = 3 I would use
vector < vector < vector < int > > > array;
But I don't know the dimension at compile time.
Use a one-dimensional array, and fake the other dimensions using multiplication of offsets for indexing, and you can pass the dimension sizes in by vector, i.e.
std::vector<int> create_md_array(const std::vector<int> & dimensions)
{
int size = std::accumulate(dimensions.begin(), dimensions.end(), 1, std::multiplies<int>());
return std::vector<int>(size);
}
You have a couple of choices. You can implement it yourself, basically multiplying the coordinates by the sizes to linearize the multi-dimensional address, and just have a simple std::vector<whatever> to hold the data.
Alternatively, you could use std::valarray and friends to accomplish the same. It has a set of classes that are specifically intended for the kind of situation you describe -- but they're used so rarely that almost nobody understands them. Writing the code yourself stands a good chance of being easier for most people to read and understand.

Convert std::vector to array

I have a library which expects a array and fills it. I would like to use a std::vector instead of using an array. So instead of
int array[256];
object->getArray(array);
I would like to do:
std::vector<int> array;
object->getArray(array);
But I can't find a way to do it. Is there any chance to use std::vector for this?
Thanks for reading!
EDIT:
I want to place an update to this problem:
I was playing around with C++11 and found a better approach. The new solution is to use the function std::vector.data() to get the pointer to the first element.
So we can do the following:
std::vector<int> theVec;
object->getArray(theVec.data()); //theVec.data() will pass the pointer to the first element
If we want to use a vector with a fixed amount of elements we better use the new datatype std::array instead (btw, for this reason the variable name "array", which was used in the question above should not be used anymore!!).
std::array<int, 10> arr; //an array of 10 integer elements
arr.assign(1); //set value '1' for every element
object->getArray(arr.data());
Both code variants will work properly in Visual C++ 2010. Remember: this is C++11 Code so you will need a compiler which supports the features!
The answer below is still valid if you do not use C++11!
Yes:
std::vector<int> array(256); // resize the buffer to 256 ints
object->getArray(&array[0]); // pass address of that buffer
Elements in a vector are guaranteed to be contiguous, like an array.