vector resize from within function

Discussion in 'C++' started by mj, Mar 5, 2006.

  1. mj

    mj Guest

    Hi,

    I recently have found it necessary to move from fortran to c++ for
    scientific programming... 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?

    Thanks for your help!

    Matt
     
    mj, Mar 5, 2006
    #1
    1. Advertising

  2. mj

    Guest

    The issue with vectors is that they are guarenteed to have contiguous
    storage. So they are not very well suited for things that will be
    resized unless you know the maximum size beforehand. If you know
    the size, use reserve so that resize will not trigger subsequent
    memory allocations.

    I would try to find something already implemented and use that. Take
    a look at valarray which is part of the standard library and also
    boost's multi-array to see if they serve your purposes.

    mj wrote:
    > Hi,
    >
    > I recently have found it necessary to move from fortran to c++ for
    > scientific programming... 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?
    >
    > Thanks for your help!
    >
    > Matt
     
    , Mar 5, 2006
    #2
    1. Advertising

  3. mj wrote:
    > I recently have found it necessary to move from fortran to c++ for
    > scientific programming...


    Let me just pause here, and say, wow! It took you _so_ long?...

    > 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?


    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
     
    Victor Bazarov, Mar 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,821
  2. Jason Heyes
    Replies:
    8
    Views:
    728
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    567
    James Kanze
    Sep 4, 2007
  5. Replies:
    2
    Views:
    1,266
    Adrienne
    Feb 13, 2005
Loading...

Share This Page