C++ dynamic arrays

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
push, pop, etc... I never did find a good description of them or when I should use them. Could someone enlighten me on how to use dynamic arrays in C++? Just something like having a default array of size (1,2), filling it with data, then resizing it to (2,2) and preserving (1,2)?

TIA

(recycled thread)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
When all else fails, RTFM:

---
ReDim Statement (Visual Basic Concepts)

Only the upper bound of the last dimension in a multidimensional array can be changed when you use the Preserve keyword; if you change any of the other dimensions, or the lower bound of the last dimension, a run-time error occurs. Thus, you can use code like this:

ReDim Preserve Matrix(10, UBound(Matrix, 2) + 1)

But you cannot use this code:

ReDim Preserve Matrix(UBound(Matrix, 1) + 1, 10)
---

---
ReDim (language ref - statements)

If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array. The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.

ReDim X(10, 10, 10)
. . .
ReDim Preserve X(10, 10, 15)

Similarly, when you use Preserve, you can change the size of the array only by changing the upper bound; changing the lower bound causes an error.
---

Read The Friendly Manual, the Manual is your Friend!
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
That's one reason C++ developers look down on it :)

VB wasn't so much designed as it was lashed together with chewing gum, duct tape and baling wire.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
(I got the program working how I want it to in VB6 by simply switching the dimensions.)

This project proved to be way too complicated in C, at least for my knowledge of it. Dynamic arrays.........argh. I did try it though, but quickly gave up after failing with realloc(), then I had (and still have) no clue how to use the dynamic arrays in C++. push, pop, etc... I never did find a good description of them or when I should use them. Could someone enlighten me on how to use dynamic arrays in C++?

This is for my mom's work so I'd rather code it in something I'm confident in anyway. ;) Regardless I'll have to learn how to do it in C++ some day, so it's much appreciated if someone could offer an explanation of dynamic arrays in C++. I'll recycle this thread.

TIA
 

Axoliien

Senior member
Mar 6, 2002
342
0
0
Yeah, as they said check out the standard template library for help with implementation. To understand them, however, you should look at the theory. There are lots of ways to make dynamic arrays, including queues, stacks, linked lists, hash lists, etc. Each has their own specialty for implemenation, so it's good to know why and when to use them. To just get something working, they are correct that you can use the STL for pretty much anything you need, with some minor changes. To make your program more efficient and optimized, understand the resource and time requirements for each type of dynamic array, and implement the one that most resembles your needs and your resource availablility. Good luck!

For some help, google search some of those key words (including STL) and read up on their usage :)
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Yeah, the terms of STL are just confusing to me. Maybe I need to learn about stacks too. Not sure about iterators either.

Is there something simple like darray.add(x,x) darray.del(x,x) darray.set(x,x) or am I grasping for straws?

Here's some code I found on the net:

vector<int> vec_one;

int a = 2; // Some integer data objects
int b = -5;

vec_one.push_back(a); // Add item at end of vector
vec_one.push_back(9);
vec_one.push_back(b);

Alright, this is good, I understand it well. Basically push_back=Add (to the end). Right now this vector has one dimension. How do I add another dimension to it? vector<vec_one> vec_two or something?

My main program code is good enough so that I don't have to remove anything, but I'd still like to know: Pop_back only resizes the array down one, just removing the one on the end, right? And what about removing something in the middle?

And when am I supposed to use an iterator? Only for the erase() and sort() funtions? If I don't use them I don't need to use the iterator begin() function?

Edit: lol, wow, I guessed right about the 2D thing. Never mind I think I got it covered. Thanks guys.

http://www.codeguru.com/forum/showthread.php?t=231046
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
build your own linked list class. Not too hard. (or use the data struct the STL.)
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,002
126
I use Maps whenever I can, that way I don't have to use the likes of push to add data. I just do a direct index with the new value and STL automatically puts the data where I like.

The best part is that the indexes can be arbitrary ("apple", "car", "tractor", etc) and don't have to be in the range 0 to n-1 like regular arrays do.