| re: vector resize from within function
mj wrote:[color=blue]
> I recently have found it necessary to move from fortran to c++ for
> scientific programming...[/color]
Let me just pause here, and say, wow! It took you _so_ long?...
[color=blue]
> I'm working on a program that needs to
> resize a 2d vector of vectors within a function... This variable
> "tri" is an input arg to my function using the syntax:
>
> function(vector<vector<int> >& tri)
>
> The problem occurs when the tri 'matrix' is resized to triple or
> quadruple the originally allocated size (which I guess requires a
> complete reallocation and consequently, a change in the memory
> address). Just having to deal with a slew of memory allocation
> issues, and I was wondering if there is a good way to resize this 2d
> vector other
> than defining a class to contain the tri variable, and writing a
> resize function within the class?[/color]
You might consider (a) reserving the size to what it should be so
there is no need to reallocate when you resize, or (b) use some
other structure, like a single-dimentional "vector" with the two-
dimensional interface, it's easier to resize, maybe.
Generally speaking, instead of wrapping your 'tri' in a class, do
write just a stand-alone function for resizing. There is no need
to roll out your own class just for the sake or resizing. Of course
using some pre-defined matrix class (I am sure you can find a slew
of them on the 'net) might prove beneficial. But I've never heard
of matrices changing (growing) their sizes during any operation.
V
--
Please remove capital As from my address when replying by mail |