2-dimensional vector (using STL)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Koen De Wolf

    2-dimensional vector (using STL)

    Dear all,

    I created a 2 dimensional vector structure that contains some integer
    values. The dimensions are dynamic.

    When I want to add values to it, using [r][c], I always get an "Access
    violation" error.

    Can someone point me in a direction to solve this problem? The source is
    given below.

    Many thanks,

    Koen



    /* header file */

    #include <vector>
    using namespace std ;

    class CFrame {
    int height, width;
    vector< vector<int> > Y;
    public:
    CFrame();
    CFrame(int h,int w, int * buffer);
    };

    /* cpp file */

    #include "CFrame.h"
    #include <vector>
    CFrame::CFrame( ){
    width = 0;
    height = 0;
    }
    CFrame::CFrame( int width, int height, int * buffer){
    this->width = width;
    this->height = height;

    for (int i=1; i <height;i++)
    for (int j=0; j<width; j++)
    {
    Y[i][j] = buffer [i * j + j];
    }
    }


  • Jon Bell

    #2
    Re: 2-dimensional vector (using STL)

    In article <bqfao9$nq8$1@g audi2.UGent.be> ,
    Koen De Wolf <Koen@raadselwe b.net> wrote:[color=blue]
    >
    >I created a 2 dimensional vector structure that contains some integer
    >values. The dimensions are dynamic.
    >
    >When I want to add values to it, using [r][c], I always get an "Access
    >violation" error.
    >
    >/* header file */
    >
    >#include <vector>
    >using namespace std ;
    >
    >class CFrame {
    > int height, width;
    > vector< vector<int> > Y;[/color]

    The vector Y has zero size, which means that you cannot use the []
    operator either to retrieve or store data. Since it appears you know in
    advance how much data is going to go into the vector, you should set its
    size in the constructor before putting data into it.
    [color=blue]
    > public:
    > CFrame();
    > CFrame(int h,int w, int * buffer);
    >};[/color]
    [snip]

    Set the size in the constructor's initializer list.
    [color=blue]
    >CFrame::CFrame (int width, int height, int * buffer){[/color]

    CFrame::CFrame (int width, int height, int * buffer)
    : Y (height, vector<int>(wid th)) {
    [color=blue]
    > this->width = width;
    > this->height = height;
    >
    > for (int i=1; i <height;i++)
    > for (int j=0; j<width; j++)
    > {
    > Y[i][j] = buffer [i * j + j];
    > }
    >}[/color]

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

    Comment

    • Koen De Wolf

      #3
      Re: 2-dimensional vector (using STL)

      Hi Jon,

      Thank you for your solution.

      I also tried this:

      frame.reserve(h eight);
      for (int i=1; i <height;i++)
      frame[i].reserve(width) ;

      But that didn't work out either.

      kind regards,

      Koen



      "Jon Bell" <jtbellq2f@pres by.edu> wrote in message
      news:bqfkds$gbk $2@jtbell.presb y.edu...[color=blue]
      > In article <bqfao9$nq8$1@g audi2.UGent.be> ,
      > Koen De Wolf <Koen@raadselwe b.net> wrote:[color=green]
      > >
      > >I created a 2 dimensional vector structure that contains some integer
      > >values. The dimensions are dynamic.
      > >
      > >When I want to add values to it, using [r][c], I always get an "Access
      > >violation" error.
      > >
      > >/* header file */
      > >
      > >#include <vector>
      > >using namespace std ;
      > >
      > >class CFrame {
      > > int height, width;
      > > vector< vector<int> > Y;[/color]
      >
      > The vector Y has zero size, which means that you cannot use the []
      > operator either to retrieve or store data. Since it appears you know in
      > advance how much data is going to go into the vector, you should set its
      > size in the constructor before putting data into it.
      >[color=green]
      > > public:
      > > CFrame();
      > > CFrame(int h,int w, int * buffer);
      > >};[/color]
      > [snip]
      >
      > Set the size in the constructor's initializer list.
      >[color=green]
      > >CFrame::CFrame (int width, int height, int * buffer){[/color]
      >
      > CFrame::CFrame (int width, int height, int * buffer)
      > : Y (height, vector<int>(wid th)) {
      >[color=green]
      > > this->width = width;
      > > this->height = height;
      > >
      > > for (int i=1; i <height;i++)
      > > for (int j=0; j<width; j++)
      > > {
      > > Y[i][j] = buffer [i * j + j];
      > > }
      > >}[/color]
      >
      > --
      > Jon Bell <jtbellap8@pres by.edu> Presbyterian College
      > Dept. of Physics and Computer Science Clinton, South Carolina USA[/color]


      Comment

      • Karl Heinz Buchegger

        #4
        Re: 2-dimensional vector (using STL)

        Koen De Wolf wrote:[color=blue]
        >
        > Hi Jon,[/color]

        Please don't top post. Put your reply beneath the thing
        you are replying to and snip away parts you don't noeed in your
        reply. Thank you.
        [color=blue]
        >
        > Thank you for your solution.
        >
        > I also tried this:
        >
        > frame.reserve(h eight);
        > for (int i=1; i <height;i++)
        > frame[i].reserve(width) ;
        >
        > But that didn't work out either.[/color]

        You wan't resize, not reserve.


        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Rolf Magnus

          #5
          Re: 2-dimensional vector (using STL)

          Koen De Wolf wrote:
          [color=blue]
          > I also tried this:
          >
          > frame.reserve(h eight);
          > for (int i=1; i <height;i++)
          > frame[i].reserve(width) ;
          >
          > But that didn't work out either.[/color]

          reserve only makes sure the vector has enough space for the specified
          amout of elements, but doesn't create those elements. Use resize()
          instead, which will default-initialize the elements.

          Comment

          Working...