[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Multidimensional Array

Posted on 1999-11-19
16
Medium Priority
?
530 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:tpe91
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 11

Expert Comment

by:mikeblas
ID: 2221465
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
 
LVL 3

Expert Comment

by:Try
ID: 2221782
I'll write you the sample code for free, but I will NOT do it while "mikeblas" has the question locked.
0
 

Author Comment

by:tpe91
ID: 2222082
I need code!
Since I already have tried what he mentioned I need a working example.
Anyway, thanks.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 

Accepted Solution

by:
Surfer earned 150 total points
ID: 2222205
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2222226
The glaring problem with that example is its lack of support for serialization.

..B ekiM
0
 

Author Comment

by:tpe91
ID: 2222389
You can use the >> or << to serialise the arrays....
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2223550
Not with the code provided in that article, no.  You need a SerializeElements() specialization, at least.

..B ekiM
0
 
LVL 3

Expert Comment

by:Try
ID: 2224399
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
 

Author Comment

by:tpe91
ID: 2224630
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
 
LVL 3

Expert Comment

by:Try
ID: 2226692
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
 

Author Comment

by:tpe91
ID: 2226739
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2292427
> 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
 
LVL 3

Expert Comment

by:Try
ID: 2293765
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2293869
> 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
 
LVL 3

Expert Comment

by:Try
ID: 2294593
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2295520
It's hard for me to understand why you're so hostile and sarcastic.

..B ekiM
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question