Initializing vector<vector<int> > and other vector questions...

Discussion in 'C++' started by pmatos, May 4, 2005.

  1. pmatos

    pmatos Guest

    Hi all,

    I have a vector of vector of ints, I could use C approach by using
    int[][] but I think C++ vector<vector<int> > would be easier to manage.
    So I have a function which creates and initializes the vector with the
    values I need (I know these values before hand).

    - What's the best way to initialize the vector<vector<int> >? Can I
    initilize it by enumerating its values?
    - If I do: v = new vector<vector<int> >(3) for example, is it
    initializing the internal vector automatically?
    - Is returning a vector from a function too heavy from an efficiency
    perspective?
    Or it would be better to return always a pointer to a vector?

    Cheers,

    Paulo Matos
    pmatos, May 4, 2005
    #1
    1. Advertising

  2. pmatos wrote:
    > I have a vector of vector of ints, I could use C approach by using
    > int[][] but I think C++ vector<vector<int> > would be easier to manage.
    > So I have a function which creates and initializes the vector with the
    > values I need (I know these values before hand).
    >
    > - What's the best way to initialize the vector<vector<int> >? Can I
    > initilize it by enumerating its values?


    Not really. The simplest way would probably be initialising it from
    an array of arrays. While you do have to create that array of arrays,
    but later you don't need to manage it.

    > - If I do: v = new vector<vector<int> >(3) for example, is it
    > initializing the internal vector automatically?


    Yes, it creates a vector of three empty vectors of int (provided that
    your 'v' variable is a pointer). You could add another argument to
    the initialiser and control how the "internal" vectors are initialised:

    v = new vector<vector<int> >(3, vector<int>(5, 42));

    That way it's a vector of 3 vectors of 5 int each, and ints are all
    set to have the value 42.

    > - Is returning a vector from a function too heavy from an efficiency
    > perspective?


    Not really. Your compiler probably has some kind of "return value
    optimization" implemented.

    > Or it would be better to return always a pointer to a vector?


    Depends on your needs.

    V
    Victor Bazarov, May 4, 2005
    #2
    1. Advertising

  3. pmatos

    pmatos Guest

    Victor Bazarov wrote:
    > pmatos wrote:
    > > I have a vector of vector of ints, I could use C approach by using
    > > int[][] but I think C++ vector<vector<int> > would be easier to

    manage.
    > > So I have a function which creates and initializes the vector with

    the
    > > values I need (I know these values before hand).
    > >
    > > - What's the best way to initialize the vector<vector<int> >? Can I
    > > initilize it by enumerating its values?

    >
    > Not really. The simplest way would probably be initialising it from
    > an array of arrays. While you do have to create that array of

    arrays,
    > but later you don't need to manage it.



    Thanks for you previous answers...
    This issue is insteresting. Do you mean that if I create a int[][],
    then I can use it to initialize the vector<vector<int> >? If I have a =
    int[][] well initialized of course can I do v = vector<vector<int> >(a)
    ? (Being both v and a pointers of course.)

    Paulo Matos
    pmatos, May 4, 2005
    #3
  4. pmatos wrote:
    > ...
    > This issue is insteresting. Do you mean that if I create a int[][],
    > then I can use it to initialize the vector<vector<int> >? If I have a =
    > int[][] well initialized of course can I do v = vector<vector<int> >(a)
    > ? (Being both v and a pointers of course.)


    No. Unfortunately, it doesn't work like that. You would still need
    to do some dancing around.

    After some thinking, I now conclude that I should take it back, there
    is no easy way to initialise a vector of vectors from a two-dimensional
    array. A vector can be initialised from an array by

    vector<int> vi(array_of_int,
    array_of_int + sizeof(array_of_int)/sizeof(int) );

    but for a vector of vectors you'd have to write that initialiser for
    each vector separately, which makes it not worth your while, probably.

    V
    Victor Bazarov, May 4, 2005
    #4
  5. pmatos

    pmatos Guest


    > No. Unfortunately, it doesn't work like that. You would still need
    > to do some dancing around.
    >
    > After some thinking, I now conclude that I should take it back, there
    > is no easy way to initialise a vector of vectors from a

    two-dimensional
    > array. A vector can be initialised from an array by
    >
    > vector<int> vi(array_of_int,
    > array_of_int + sizeof(array_of_int)/sizeof(int)

    );
    >
    > but for a vector of vectors you'd have to write that initialiser for
    > each vector separately, which makes it not worth your while,

    probably.
    >


    Well, yeah, It seems I'll have to give it some thinking then. Thanks a
    lot for the help.

    Paulo Matos

    > V
    pmatos, May 4, 2005
    #5
  6. pmatos

    John Carson Guest

    "Victor Bazarov" <> wrote in message
    news:yY8ee.67894$01.us.to.verio.net
    > pmatos wrote:
    >
    >> - If I do: v = new vector<vector<int> >(3) for example, is it
    >> initializing the internal vector automatically?

    >
    > Yes, it creates a vector of three empty vectors of int (provided that
    > your 'v' variable is a pointer). You could add another argument to
    > the initialiser and control how the "internal" vectors are
    > initialised:
    > v = new vector<vector<int> >(3, vector<int>(5, 42));
    >
    > That way it's a vector of 3 vectors of 5 int each, and ints are all
    > set to have the value 42.


    Isn't one of the main reasons for using vector that it does memory
    management for you? Is it not therefore almost always a bad idea to be
    creating vectors using new?

    --
    John Carson
    John Carson, May 5, 2005
    #6
  7. pmatos

    reut

    Joined:
    Apr 26, 2007
    Messages:
    1
    push_back

    how can I use push_back in vector< vector<int> >
    ?
    reut, Apr 26, 2007
    #7
    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. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    450
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    942
    Jim Langston
    Oct 20, 2005
  5. jdm
    Replies:
    1
    Views:
    661
    Victor Bazarov
    May 18, 2010
Loading...

Share This Page