Dynamic 2d array of CString

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster

Dynamic 2d array of CString

 
0
  #1
Jul 24th, 2008
I am trying to create a 2d array in my program that can hold CStrings. I need to be able to add my CString to any row and column in the array(not just at the end) and I need it to be able to grow in size. Does anyone know how I could do this?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

Re: Dynamic 2d array of CString

 
0
  #2
Jul 24th, 2008
An array can not be able to grow in size, it is fixed.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster

Re: Dynamic 2d array of CString

 
0
  #3
Jul 24th, 2008
Let me explain in more detail, I have:
int numData;
CStringArray MyArray[50];

I want to be able to set the value of any element like this:

MyArray[rownumber].SetAtGrow(colnumber, value);

Is there any way at the beginning I can set it to size numData like below?

CStringArray MyArray[numData]
//this gives me an error
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Dynamic 2d array of CString

 
0
  #4
Jul 24th, 2008
C++ arrays are static in size, yes, but the Standard Template Library has containers that can do what you want. lists sounds like they may fit your needs, but vectors or deques may also be of use.

As far as this:
  1. CStringArray MyArray[numData]
You need to use dynamic memory to allocate an array to a size unknown at compile-time. Like this:
  1. CStringArray* MyArray;
  2. MyArray = new CStringArray[numData];
  3. //use the array
  4. delete[] MyArray;
If this is all you need, you can ignore the stuff about the STL (standard template library). But if you want it to grow and shrink during runtime, you'll need a list or another container.
Last edited by CoolGamer48; Jul 24th, 2008 at 5:03 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Dynamic 2d array of CString

 
0
  #5
Jul 24th, 2008
You can declare an array of type T using static memory like so:

T myArray[constInt];

where constInt resolves to an int that is declared const, meaning its value must be known at compile time and cannot be changed at run time. If you don't know what value you want/need for constInt at compile time, then you should declare the array on the heap, using dynamic rather than static memory. That is, you can do this:

int size;
cout << "enter value for size" << endl;
cin >> size;

T myArr * = new T[size];

Technically myArray and myArr aren't the same thing, but they behave the same and can be used interchangably, at least for most intents and purposes. If you don't want to monkey with memory handling yourself then using something like std::vector, which is a class that acts like a souped up array with [] operator and everything including memory handling.

CString is MSs string class that acts very much like std::string, if I'm not mistaken. Since the length of any given CString may vary I don't know whether you can do this:

CString myArray[10];

You can try it and see. Likewise you can try the dynamic declaration and see if that works, too. Otherwise, a vector of CStrings should work fine.
Last edited by Lerner; Jul 24th, 2008 at 5:06 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC