Help me convert this from C# to C++?

This topic is 2387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello there. I'm trying to convert some code on the internet from C# to C++. In the C# code, a multidimensional array is initialized without any length specified. I'm trying to achieve this in C++

C# code:

 int[,] array = new int[,] { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, };

In C++ I've been trying to recreate this by creating a multidimensional array that is blank( ie. int array[][]) but you can only leave the first unspecified.

Thanks, Matt.

Share on other sites
If the array is fixed size, just fill in the rank sizes.

If you don't know the size, use a one-dimensional array and use custom indexing such as "array[y*width+x]", a pointer or jagged array (int*[] or int**), or some kind of multidimensional array class. I personally use one-dimensional arrays/std::vectors with custom indexing.

Share on other sites

If the array is fixed size, just fill in the rank sizes.

If you don't know the size, use a one-dimensional array and use custom indexing such as "array[y*width+x]", a pointer or jagged array (int*[] or int**), or some kind of multidimensional array class. I personally use one-dimensional arrays/std::vectors with custom indexing.

Hey there. I'm not quite sure what you mean by ranks sizes. Do you mean just forget about the unspecified values and change it to int array[3][5] or whatnot? I could do this but I'd like to be able to change the size of the array without having to keep track of it. If this isn't possible then I think I'll have to do something like what you mentioned and use a single dimensional array that acts like a multidimensional one.

Share on other sites
Sorry, I used the wrong terminology in my last post. The 'rank' of an array is the number of dimensions an array has (but that wasn't quite what I was trying to say). What I meant was "You can provide the size inside all of the square brackets if you know it, and it'll make the compiler will accept that syntax."

Sorry for the confusion.

Share on other sites
if you are using a multi-dimensional array, 2D for example and you know the size you could always use a "for" loop to make them all 0

 myArray[21][51]; int i = 0; int j = 0; for(i = 0; i <= 20; i++) { for(j = 0; j <= 50; j++) { myArray[j] = 0; } }

Hope this helps

Share on other sites
Do you mean just forget about the unspecified values and change it to int array[3][5] or whatnot?

So yes, declaring the const array dimensions becomes syntactically similar to your original example:
[source lang="cpp"]int array[4][5] = { //etc as per the C# code[/source]

however

I could do this but I'd like to be able to change the size of the array without having to keep track of it. If this isn't possible then I think I'll have to do something like what you mentioned and use a single dimensional array that acts like a multidimensional one.

the C# container classes are much more like the C++ STL array/vector classes. So, for example
[source lang="cpp"]std::vector < std::vector < int > > container;[/source]
allows you to size (and query!) the dimensions, and still treat the container like a (resizeable) multidimensional array (container[x][y]).

In my experience the performance tradeoff is usually negligible, and the code easier to read than container[x + width*y] or what have you. A single-dimension array though is going to be far superior if you're simply scanning through everything (*container++ wins hands down), so the best answer is always going to be application dependent.

Share on other sites
It is fairly trivial to write a wrapper for std::vector which allows you to use a contiguous allocation as a multi-dimensional array.

Something like:
 template<class T> class vector2d{ public: vector2d(int width, int height) : storage(width * height), width_(width) { } T &operator()(int x, int y) { return storage[index(x,y)]; } const T &operator()(int x, int y) const { return storage[index(x,y)]; } int width() const { return width_; } int height() const { return storage.size() / width_; } private: typename std::vector<T>::size_type index(int x, int y) { // range check here if you want // the vector usually does this for us in debug anyway return y * width_ + x; } std::vector<T> storage; int width_; }; 
The main benefit of this is that the matrix is kept rectangular, while nested vectors can be jagged.

• 22
• 10
• 19
• 15
• 14