Vector of vector question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BCC

    Vector of vector question

    If I create a vector of vectors of double:

    std::vector< std::vector<dou ble> > table1;

    Are my vectors of doubles uninitialized? Do I have to loop through table1
    and initialize each vector of doubles using new?

    And in cleaning up, manually delete each of these vectors of doubles?

    Thanks,
    B



  • Cy Edmunds

    #2
    Re: Vector of vector question

    "BCC" <bryan@akanta.c om> wrote in message
    news:gvHMb.9111 $wP1.6082@newss vr27.news.prodi gy.com...[color=blue]
    > If I create a vector of vectors of double:
    >
    > std::vector< std::vector<dou ble> > table1;
    >
    > Are my vectors of doubles uninitialized?[/color]


    No. They are default initialized. You have an empty vector of empty vectors.
    [color=blue]
    > Do I have to loop through table1
    > and initialize each vector of doubles using new?[/color]

    No. Please.
    [color=blue]
    >
    > And in cleaning up, manually delete each of these vectors of doubles?[/color]

    No again. std::vector is a well designed class that doesn't require a lot of
    handholding.
    [color=blue]
    >
    > Thanks,
    > B
    >
    >
    >[/color]



    --
    Cy



    Comment

    • Donovan Rebbechi

      #3
      Re: Vector of vector question

      In article <gvHMb.9111$wP1 .6082@newssvr27 .news.prodigy.c om>, BCC wrote:[color=blue]
      > If I create a vector of vectors of double:
      >
      > std::vector< std::vector<dou ble> > table1;
      >
      > Are my vectors of doubles uninitialized?[/color]

      They're empty. table1.size() will produce 0 and table1[0].size results in
      undefned behaviour (since there is no first element yet)
      [color=blue]
      > Do I have to loop through table1
      > and initialize each vector of doubles using new?[/color]

      You don't use new, the vector class manages its own storage. You use vector
      member functions. If you want the vectors to have some entries, you need to
      do something like this:
      typedef std::vector<dou ble>::size_type dvecsize;
      dvecsize m = 10, n = 5;
      std::vector< std::vector<dou ble> > table1 (m,n);
      std::cout << table1.size() << std::endl; // 10
      std::cout << table1[0].size() << std::endl; // 5
      [color=blue]
      > And in cleaning up, manually delete each of these vectors of doubles?[/color]

      No, the destructor of the vector takes care of deallocating storage. That's
      the main point of having a vector.

      Cheers,
      --
      Donovan Rebbechi

      Comment

      • Daniel T.

        #4
        Re: Vector of vector question

        "BCC" <bryan@akanta.c om> wrote:
        [color=blue]
        > If I create a vector of vectors of double:
        >
        > std::vector< std::vector<dou ble> > table1;
        >
        > Are my vectors of doubles uninitialized? Do I have to loop through table1
        > and initialize each vector of doubles using new?
        >
        > And in cleaning up, manually delete each of these vectors of doubles?[/color]

        Any doubles created by the vector will be initialized to 0.0, they don't
        need to be 'new'ed nor 'delete'ed.

        Are you sure you want to use a vector of vectors? I would only do that
        if I needed a ragged array. If the array represents a table, you would
        be better off creating a 2D array class. See the FAQ for a sample
        implementation.

        Comment

        • Jon Bell

          #5
          Re: Vector of vector question

          In article <gvHMb.9111$wP1 .6082@newssvr27 .news.prodigy.c om>,
          BCC <bryan@akanta.c om> wrote:[color=blue]
          >If I create a vector of vectors of double:
          >
          >std::vector< std::vector<dou ble> > table1;
          >
          >Are my vectors of doubles uninitialized?[/color]

          In fact, at this point, you have *no* vector<double>s at all. The "outer"
          vector that is supposed to contain vector<double>s has zero size. No
          memory has been allocated at all for storing vector<double>s .
          [color=blue]
          > Do I have to loop through table1
          >and initialize each vector of doubles using new?[/color]

          Assuming you know how big the table is supposed to be (numRows x numCols)
          at run time, before you declare the table, the easiest way is to make the
          table the appropriate size when you declare it:

          std::vector<std ::vector<double > > table1 (numRows,
          std::vector<dou ble>(numCols));

          Then fill the table using the usual table1[row][col] notation.
          [color=blue]
          >And in cleaning up, manually delete each of these vectors of doubles?[/color]

          No, std::vector's destructor will take care of any cleanup that is
          necessary, in this case. If you had declared a vector of pointers, then
          you would need to either delete the pointers individually or make sure
          other pointers are pointing to the objects being pointed to, before the
          vector goes out of scope. But you still wouldn't have to worry about
          deleting the vector itself, because you didn't use new to create it.

          --
          Jon Bell <jtbellm4h@pres by.edu> Presbyterian College
          Dept. of Physics and Computer Science Clinton, South Carolina USA

          Comment

          • David Fisher

            #6
            Re: Vector of vector question

            "Daniel T." <postmaster@eat hlink.net> wrote:
            [color=blue]
            > Are you sure you want to use a vector of vectors? I would only do that
            > if I needed a ragged array. If the array represents a table, you would
            > be better off creating a 2D array class. See the FAQ for a sample
            > implementation.[/color]

            The reference is
            http://www.parashift.com/c++-faq-lit...html#faq-16.17 if you
            didn't already have it.

            David F


            Comment

            • Ron Natalie

              #7
              Re: Vector of vector question


              "BCC" <bryan@akanta.c om> wrote in message news:gvHMb.9111 $wP1.6082@newss vr27.news.prodi gy.com...[color=blue]
              > If I create a vector of vectors of double:
              >
              > std::vector< std::vector<dou ble> > table1;
              >
              > Are my vectors of doubles uninitialized? Do I have to loop through table1
              > and initialize each vector of doubles using new?[/color]

              There are no elements to initialize, you've created an empty vector of empty
              vectors. However, if you were to give it a size arg (or resize it), then absent
              an explicit value to the constructor or resize call, it will provide default initialized
              values.

              [color=blue]
              > And in cleaning up, manually delete each of these vectors of doubles?[/color]

              No, the vector will take all the elements with them when they go.

              Comment

              • David Harmon

                #8
                Re: Vector of vector question

                On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
                <postmaster@eat hlink.net> was alleged to have written:[color=blue]
                >Are you sure you want to use a vector of vectors? I would only do that
                >if I needed a ragged array. If the array represents a table, you would
                >be better off creating a 2D array class.[/color]

                Well, I can't entirely agree. A vector of vectors is a quick and
                cheerful way of getting a table sized at run time without having to
                reinvent the wheel.

                vector< vector<double> > table1(rows, vector<double>( columns));

                Comment

                • Gavin Deane

                  #9
                  Re: Vector of vector question

                  "David Fisher" <nospam@nospam. nospam.nospam> wrote in message news:<jjIMb.715 8$xm.336991@nas al.pacific.net. au>...[color=blue]
                  > "Daniel T." <postmaster@eat hlink.net> wrote:
                  >[color=green]
                  > > Are you sure you want to use a vector of vectors? I would only do that
                  > > if I needed a ragged array. If the array represents a table, you would
                  > > be better off creating a 2D array class. See the FAQ for a sample
                  > > implementation.[/color]
                  >
                  > The reference is
                  > http://www.parashift.com/c++-faq-lit...html#faq-16.17 if you
                  > didn't already have it.
                  >
                  > David F[/color]

                  The next FAQ shows the same thing using a vector of vectors to
                  implement the 2D array class.



                  Removes all the need for explicit memory management in the class. And
                  it should be easy to design the class so that it's impossible for the
                  individual vectors-within-a-vector to end up with different sizes.

                  --
                  hth
                  GJD

                  Comment

                  • Gavin Deane

                    #10
                    Re: Vector of vector question

                    David Harmon <source@netcom. com> wrote in message news:<4020bc2c. 84703842@news.w est.earthlink.n et>...[color=blue]
                    > On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
                    > <postmaster@eat hlink.net> was alleged to have written:[color=green]
                    > >Are you sure you want to use a vector of vectors? I would only do that
                    > >if I needed a ragged array. If the array represents a table, you would
                    > >be better off creating a 2D array class.[/color]
                    >
                    > Well, I can't entirely agree. A vector of vectors is a quick and
                    > cheerful way of getting a table sized at run time without having to
                    > reinvent the wheel.
                    >
                    > vector< vector<double> > table1(rows, vector<double>( columns));[/color]

                    The potential problem is that careless code could end up altering the
                    sizes of some of the vector<double>s . Depending on your application,
                    you might want the robustness of a class that does not allow this to
                    happen.

                    --
                    GJD

                    Comment

                    • Daniel T.

                      #11
                      Re: Vector of vector question

                      deane_gavin@hot mail.com (Gavin Deane) wrote:
                      [color=blue]
                      > "David Fisher" <nospam@nospam. nospam.nospam> wrote in message
                      >[color=green]
                      > > "Daniel T." <postmaster@eat hlink.net> wrote:
                      > >[color=darkred]
                      > > > Are you sure you want to use a vector of vectors? I would only do that
                      > > > if I needed a ragged array. If the array represents a table, you would
                      > > > be better off creating a 2D array class. See the FAQ for a sample
                      > > > implementation.[/color]
                      > >
                      > > The reference is
                      > > http://www.parashift.com/c++-faq-lit...html#faq-16.17 if you
                      > > didn't already have it.
                      > >
                      > > David F[/color]
                      >
                      > The next FAQ shows the same thing using a vector of vectors to
                      > implement the 2D array class.
                      >
                      > http://www.parashift.com/c++-faq-lit...html#faq-16.18
                      >
                      > Removes all the need for explicit memory management in the class. And
                      > it should be easy to design the class so that it's impossible for the
                      > individual vectors-within-a-vector to end up with different sizes.[/color]

                      A better choice, of course would be to implement the 2D array using a
                      single vector. This also removes the need for explicit memory management
                      and is easer to design...

                      Comment

                      • RanggaPratama

                        #12
                        Re: Vector of vector question

                        Now, how do we initialize the 2d vector?

                        I tried to intialize the 2d vector by doing:(but, it's not working)

                        for(int i = 0; i < row; i++)
                        for(int j = 0; j < col; j++)
                        table1[i].push_back(1);

                        I knew it wouldn't work. I was just trying to do different things.

                        I was thinking of using iterator, but I don't have any idea how to do it with 2d vector.

                        Comment

                        • Clark Cox

                          #13
                          Re: Vector of vector question

                          In article
                          <17db7dfb5ad332 6db75c954a1ab97 081@localhost.t alkaboutprogram ming.com>,
                          "RanggaPrat ama" <madrasah2@hotm ail.com> wrote:
                          [color=blue]
                          > Now, how do we initialize the 2d vector?
                          >
                          > I tried to intialize the 2d vector by doing:(but, it's not working)
                          >
                          > for(int i = 0; i < row; i++)
                          > for(int j = 0; j < col; j++)
                          > table1[i].push_back(1);
                          >
                          > I knew it wouldn't work. I was just trying to do different things.[/color]

                          You're not doing anything to change the size of the outer vector.
                          Since you said "initialize , I assume that the vector is empty to begin
                          with, so table1[i] is undefined.
                          [color=blue]
                          > I was thinking of using iterator, but I don't have any idea how to do it with
                          > 2d vector.
                          >[/color]

                          How about (where T is whatever type the inner vector contains):

                          {
                          table1.clear();
                          table1.resize(r ow, std::vector<T>( col, 1));
                          }

                          Comment

                          Working...