# Dynamic (Numeric) Arrays

• 04-19-2006
DavidB
Dynamic (Numeric) Arrays
Just joined this forum last night. Found it by way of the STL tutorial elsewhere on this site:

The STL Vector Class

I happen to be just getting my feet wet when it comes to dynamic arrays. All my programming involves arrays of numerical data, usually type double, and generally includes 1D and 2D arrays, nothing with more dimensions. Learning these concepts is proving to be quite tricky; finding a decent tutorial is proving to be much more difficult than I thought.

Does anybody here know where I can find a decent tutorial, with source code snippets, that teaches a person how to use <vector> to create 1D and 2D arrays? The programs I plan to write will be quite large, and the arrays will be passed in and out of sub-routines frequently.

Thanks.

David
• 04-19-2006
DougDbug
Well... I have a really good STL book: The C++ Standard Library, by Nicolai M. Josuttis.

I'm a little "rusty" on this stuff, so I don't have any specific suggestions about your 2d / 3d vectors.... Make that "very rusty!"
• 04-19-2006
Daved
I'm not sure that you'll need much help beyond the examples in that tutorial. You have to think of vector<double> as a type like any other. So a 2D array of doubles is really just a vector of vector<double>. And you can pass a vector<double> to a function like you would any other variable that you wanted to pass to a function. Generally, with vectors that is done as a const reference if the vector won't be modified by the function, and as a non-const reference if it will.

cppreference.com or the sgi STL site might have more information to help you out, and Josuttis' The C++ Standard Library is probably the best reference for the standard library containers and algorithms.
• 04-19-2006
There is one little gotcha. You have to leave a space between the closing angled brackets or the compiler will think you are trying to use the >> operator rather than closing a vector of vectors.

std::vector<vector<int> > myVectorOfVectorOfInts;
• 05-03-2006
DavidB
Thanks for all the feedback.

I now have a working program. One question I wondered about, but didn't post: does the section of code in which vector variables are resized have to be wrapped in a try block? I found an answer and thought I'd share it in case others have the same question: Yes, it does.

<vector> does no exception catching internally when trying to allocate space for, say, a 1000000x1000000 2D array.
• 05-03-2006
Daved
Normally, you wouldn't want to handle an out-of-memory exception everywhere in your code, you would have one or more higher-level functions that can handle that exception relatively gracefully. That function would have the try/catch block around the code that does the bulk of the work.

So you wouldn't actually put the resize() into a try/catch block any more than you'd put any call to new, any declaration of a string object, vector, or any other container, any call to push_back, or any use of your own classes and functions that use dynamic memory allocation inside its own try/catch block. That may have been what you meant, but I just wanted to clarify. Any construction, insertion or resize on a vector can cause a memory allocation which can throw an exception.

Of course, if you know that a particular call to resize has a high likelihood of generating such an exception (like in your 1000000x1000000 example), then that is one case where it may make sense to put it specifically into its own try/catch block.