Multidimensional Array

Hallo,
I need help. How Can I create a multidimensional array with CArray or CSortedArray?
Is it possible to create an nxm array where n,m are variables?

Thanl you

Peter
tpe91Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikeblasCommented:
Yes. Just create an array of your arrays.

Do you want a code sample? I don't usually write samples for only 50 points.

..B ekiM
0
TryCommented:
I'll write you the sample code for free, but I will NOT do it while "mikeblas" has the question locked.
0
tpe91Author Commented:
I need code!
Since I already have tried what he mentioned I need a working example.
Anyway, thanks.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

SurferCommented:
I had the same question last year and found this code.  I think it is what you are looking for. You may want to change/add some helper  functions/operators but this should get you going.  Good luck.


http://codeguru.developer.com/mfc/faq/comments/67.shtml

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikeblasCommented:
The glaring problem with that example is its lack of support for serialization.

..B ekiM
0
tpe91Author Commented:
You can use the >> or << to serialise the arrays....
0
mikeblasCommented:
Not with the code provided in that article, no.  You need a SerializeElements() specialization, at least.

..B ekiM
0
TryCommented:
Whenever I need to create multi-dimensional arrays, I always follow this path, deviating only with some variations.  Of course there is always the alternative way of just using an ordinary C/C++ array, but why duplicate a lot of the work when MFC has already created ways for making this a breeze (viz. a CArray of CArrays).

This example is for a 2-Dimensional array, even though you can extend the idea for more dimensions.

The code (below) creates a type for an array of integers (viz. CIntArray) and another for an array of "CIntArrays".  The array of arrays is called, "CIntArray2D".  Then it derives a class from CIntArray2D named, "CIntMatrix".  This derived class has a constructor that sets the sizes of each array to hold the specified number of rows and columns.


#include <afxtempl.h>
typedef  CArray<int, int> CIntArray;
typrdef  CArray<CIntArray, CIntArray> CIntArray2D;

class CIntMatrix  :  public CIntArray2D
{
public:
    CIntMatrix(int r, int c)
    {
       SetSize(r);
       for(int i=0; i < r; i++)
           ElementAt(i).SetSize(c);
    }
};


Notice, I was still able to use the functions that CArray has for performing some of the operations in setting up my arrays.  Personally, I prefer this way because it gives me a lot of flexibility with what I might want to do with my arrays (be them one-dimensional, or multi-dimensional).


There is another way I could have used, because collection classes created with CArray (also CList and CMap), use helper functions to perform many of the operation involving the objects in the collection.  Some of these helper functions are, ConstructElements(), DestructElements(), CopyElements(), SerializeElements(), etc., which perform similar functions to what you might do if you were to choose the other method I chose earlier.

Note that these helper functions are global functions that do NOT belong to any specific class.  This also means that you only need to implement them once for each data type you would be using.

For example, using the SerialElements() function, the collection templates will call this function to serialize the objects in the collection.  The default implementation simply does a bitwise copy, and (looking at the sample below) you see parameters for the first element, a count of elements, and a reference to CArchive used for serialization.  (NOTE:  If your object is derived from CObject, you can use the Serialize() function of your element class.)

void  SerialElements(CArchive& ar, CIntArray* pElement, int nCount)
{
   int  nIndex;
   for(nIndex = 0; nIndex < nCount; nIndex++, pElement++)
       pElement->Serialize(ar);
}

------------------------------------

For the ConstructElements() function, it would be:

void  ConstructElements(CIntArray* pElement, int nCount)
{
   int  nIndex;
   for(nIndex = 0; nIndex < nCount; nIndex++, pElement++)
       pElement->CIntArray::CIntArray();
}


Notice, because I defined another class (viz. CMatrix) and created my own constructor in that class, I didn't need to use the ConstructElements() helper function.

Two approaches I have oultined above.  They are separate, and follow separate implementations.  In the first approach, I created my arrays (by way of CArray), but I also derived a class from my multi-dimensional array, because I wanted the flexibility of (perhaps) implementing a sort routine.

The second approach uses the global helper functions which sort of cut directly to the chase, and is more cut and dry.  They do not use the member functions of the collection classes to initialize, add, remove, insert, etc., elements.


If you have any questions, don't hesitate.
0
tpe91Author Commented:
To Try:
I think that you should taked the points :)
To everyone:

Anyway, I think that I will see it soon, if I can serialise it, or I will use Try's code. BTW, I had answered a few question in HTML area. Do you know why I did not took any points? I need them for the C++ section :-)

Cheers,
Peter
0
TryCommented:
Don't worry about the points, I didn't do it for the points.  Just happy to help.

In my example regarding SerializeElements(), I misspelt the name of the function.  Please note it is NOT "SerialElements"; the correct spelling is "SerializeElements".

If I wasn't too clear with the things I've stated, I'll be happy to explain further.

------------------------------------

NOTE:  Points that you earn when you answer someone's question, cannot be used (in return) to offer to someone else for answering your questions.
0
tpe91Author Commented:
Try: Thanks a lot,
Really, I did not said that for yourseff. Actually, I have a BIG project for my PhD and although a know C++ pretty well, I need help for the MFC. So, I am needing the points:)
Anyway, I think that the idea is to help each other. If not, then what's the point? I mean, we coud do that job in a Helpdesk, or not?

By the way, I will try your code in a few days (till Thursday).


"If I wasn't too clear with the things I've stated, I'll be happy to
explain further." eg by mail???:):):):):):):):):):):)
Cheers,
Peter
0
mikeblasCommented:
> Note that these helper functions are global
 > functions that do NOT belong to any specific class.  

Well, kind of. They're strongly associated with a particular class because they're friends to the class, and they're specializations based on the template arguments of the class.

But SerializeElements() is what was missing from that codeguru.com example. You have to do it, and do it right, if you want the code to work.

..B ekiM
0
TryCommented:
Hairsplitting!

Nonetheless, I don't believe any of us can fully express the magnitude of our thanks to you for bringing that to our attention.  I'm sure our lives are much better off now, that you have done so.
0
mikeblasCommented:
> Hairsplitting!

Hardly. There's a vast difference between what works and what doesn't work. The collection helper functions aren't easy to get right, are crucial to a working implementation, and lots of people end up asking about 'em.

I don't know why that fact offends you so, but there it is.

..B ekiM
0
TryCommented:
I don't believe anybody is more qualified than you to talk about what works and what doesn't.  You have certainly contributed more than your fair share to the pile.
0
mikeblasCommented:
It's hard for me to understand why you're so hostile and sarcastic.

..B ekiM
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.