Converting from int** to vector< vector<int> >&

Discussion in 'C++' started by jdm, May 18, 2010.

  1. jdm

    jdm Guest

    I'm trying to rewrite some code that used arrays and pointers like
    this:

    void calculate_table(size_t rows, size_t columns, int *raw_data, int
    **table)
    {
    //initialise the elements of table to zero.
    //then save the results of calculations on the elements of raw_data
    in it
    }

    so that it will use the standard library instead. Sometimes there have
    been situations where I've had to leave the original function intact,
    and so to avoid duplication have ended up writing something like:

    void calculate_table(size_t rows, size_t columns, vector <int>&
    raw_data, vector< vector <int> >& table)
    {
    int **array_table = new int*[rows];
    for (size_t s = 0; s <rows; s++)
    {
    array_table = &table[0];
    }

    calculate_table(rows, columns, &raw_data[0], array_table);
    delete[] array_table;
    }

    (exploiting the fact that the elements of a vector have to be stored
    contiguously, as per the C++ standard.)

    Is there a more efficient way of doing it than that for loop? Some of
    these functions will have to be called a very large number of times
    during some simulated annealing, and I may soon have to do something
    like the above with some code involving three-dimensional arrays.

    Thanks,

    James McLaughlin.
    jdm, May 18, 2010
    #1
    1. Advertising

  2. On 5/18/2010 1:07 PM, jdm wrote:
    > I'm trying to rewrite some code that used arrays and pointers like
    > this:
    >
    > void calculate_table(size_t rows, size_t columns, int *raw_data, int
    > **table)
    > {
    > //initialise the elements of table to zero.
    > //then save the results of calculations on the elements of raw_data
    > in it
    > }
    >
    > so that it will use the standard library instead. Sometimes there have
    > been situations where I've had to leave the original function intact,
    > and so to avoid duplication have ended up writing something like:
    >
    > void calculate_table(size_t rows, size_t columns, vector<int>&
    > raw_data, vector< vector<int> >& table)
    > {
    > int **array_table = new int*[rows];
    > for (size_t s = 0; s<rows; s++)
    > {
    > array_table =&table[0];
    > }
    >
    > calculate_table(rows, columns,&raw_data[0], array_table);
    > delete[] array_table;
    > }
    >
    > (exploiting the fact that the elements of a vector have to be stored
    > contiguously, as per the C++ standard.)
    >
    > Is there a more efficient way of doing it than that for loop? Some of
    > these functions will have to be called a very large number of times
    > during some simulated annealing, and I may soon have to do something
    > like the above with some code involving three-dimensional arrays.


    Without knowing what kind of calculations you're doing, it's difficult
    to predict the improvements that can be achieved by a certain method,
    but you could try rewriting your calculation algorithm in terms of
    iterators, which should work fine whether you store the data in arrays
    (dynamic, automatic) or in standard containers.

    I don't have an example for you. Do you have a copy of "The C++
    Standard Library"? You should...

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 18, 2010
    #2
    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,732
  2. Schnoffos
    Replies:
    2
    Views:
    1,198
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  4. Replies:
    8
    Views:
    1,894
    Csaba
    Feb 18, 2006
  5. Replies:
    6
    Views:
    1,496
    Richard Tobin
    Mar 19, 2009
Loading...

Share This Page