std::vector question

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

    std::vector question

    I havea program which on execution gives unpredictable behaviour (it
    shouldn't). In trying to track down the problem, I'm wondering if there is a
    difference between these two ways of filling a std::vector with data:

    Method 1:

    std::vector<int > v;
    int k;

    for(i=0;i<n;i++ ){
    k = i + 3;
    v.push_back(k);
    }

    Method 2:

    std::vector<int > v;

    for(i=0;i<n;i++ ){
    int k = i + 3;
    v.push_back(k);
    }


  • Victor Bazarov

    #2
    Re: std::vector question

    "Hamish" <h.dean@xtra.co .nz> wrote...[color=blue]
    >I havea program which on execution gives unpredictable behaviour (it
    > shouldn't). In trying to track down the problem, I'm wondering if there is
    > a
    > difference between these two ways of filling a std::vector with data:
    >
    > Method 1:
    >
    > std::vector<int > v;
    > int k;
    >
    > for(i=0;i<n;i++ ){
    > k = i + 3;
    > v.push_back(k);
    > }
    >
    > Method 2:
    >
    > std::vector<int > v;
    >
    > for(i=0;i<n;i++ ){
    > int k = i + 3;
    > v.push_back(k);
    > }
    >
    >[/color]

    No, in this particular case there is no difference. However, something
    tells me that your real code, the code that gives you trouble, is a bit
    more complicated than that.

    It is often important to follow certain rules in your programming. For
    example, the famous "Rule of Three" (look it up). If your class has
    some kind of dynamic memory management, you simply _must_ follow it.

    V


    Comment

    • Chris Theis

      #3
      Re: std::vector question


      "Hamish" <h.dean@xtra.co .nz> wrote in message
      news:oDoAd.3372 $mo2.171844@new s.xtra.co.nz...[color=blue]
      > I havea program which on execution gives unpredictable behaviour (it
      > shouldn't). In trying to track down the problem, I'm wondering if there is[/color]
      a[color=blue]
      > difference between these two ways of filling a std::vector with data:
      >
      > Method 1:
      >
      > std::vector<int > v;
      > int k;
      >
      > for(i=0;i<n;i++ ){
      > k = i + 3;
      > v.push_back(k);
      > }
      >
      > Method 2:
      >
      > std::vector<int > v;
      >
      > for(i=0;i<n;i++ ){
      > int k = i + 3;
      > v.push_back(k);
      > }
      >[/color]

      The only difference with these two methods is the scope of k but this
      doesn´t affect the behavior in this case. Please post your real code which
      gives you trouble.

      Cheers
      Chris


      Comment

      • KPB

        #4
        Re: std::vector question

        Victor Bazarov wrote:
        [color=blue]
        > It is often important to follow certain rules in your programming. For
        > example, the famous "Rule of Three" (look it up).[/color]

        I'm actually interested in what this "rule of three" says but I'm
        getting lots of irrelevant hits on google. It seems that everybody has
        some "rule of three".

        Any hints?

        Thanks,
        KPB




        Comment

        • Jeff Flinn

          #5
          Re: std::vector question

          KPB wrote:[color=blue]
          > Victor Bazarov wrote:
          >[color=green]
          >> It is often important to follow certain rules in your programming.
          >> For example, the famous "Rule of Three" (look it up).[/color]
          >
          > I'm actually interested in what this "rule of three" says but I'm
          > getting lots of irrelevant hits on google. It seems that everybody has
          > some "rule of three".
          >
          > Any hints?[/color]

          Googling 101: What language are you discussing? Add that to your search
          string.

          Jeff


          Comment

          • KPB

            #6
            Re: std::vector question

            Jeff Flinn wrote:
            [color=blue]
            > Googling 101: What language are you discussing? Add that to your search
            > string.[/color]

            Please don't treat me like I'm an idiot. I'm not.

            Thanks,
            KPB

            Comment

            • KPB

              #7
              Re: std::vector question

              KPB wrote:[color=blue]
              > Victor Bazarov wrote:
              >[color=green]
              >> It is often important to follow certain rules in your programming. For
              >> example, the famous "Rule of Three" (look it up).[/color]
              >
              >
              > I'm actually interested in what this "rule of three" says but I'm
              > getting lots of irrelevant hits on google. It seems that everybody has
              > some "rule of three".
              >
              > Any hints?
              >
              > Thanks,
              > KPB
              >
              >
              >
              >[/color]

              Nevermind Victor. I found them. I alredy know them. I don't think Scott
              Meyers refered to this as the "rule of three" but he did stress this
              nonetheless in his Effective C++ books.

              KPB

              Comment

              • Hamish

                #8
                Re: std::vector question

                > > I havea program which on execution gives unpredictable behaviour (it[color=blue][color=green]
                > > shouldn't). In trying to track down the problem, I'm wondering if there[/color][/color]
                is[color=blue]
                > a[color=green]
                > > difference between these two ways of filling a std::vector with data:
                > >
                > > Method 1:
                > >
                > > std::vector<int > v;
                > > int k;
                > >
                > > for(i=0;i<n;i++ ){
                > > k = i + 3;
                > > v.push_back(k);
                > > }
                > >
                > > Method 2:
                > >
                > > std::vector<int > v;
                > >
                > > for(i=0;i<n;i++ ){
                > > int k = i + 3;
                > > v.push_back(k);
                > > }
                > >[/color]
                >
                > The only difference with these two methods is the scope of k but this
                > doesn´t affect the behavior in this case. Please post your real code which
                > gives you trouble.[/color]

                I haven't been able to track down the problem yet. However, the only
                difference between an older version which worked, adn this version is the
                following data structures:

                std::vector<Pol yTypeClass*> PolyTypes;

                class PolyTypeClass{
                public:
                PolyTypeClass() ;
                virtual ~PolyTypeClass( );

                const PolyTypeClass& operator= (const PolyTypeClass& poly);
                void Copy(PolyTypeCl ass * pCopy);

                int ID;
                std::vector<Bas ePolygonClass> Rot;
                };

                class BasePolygonClas s{
                public:
                BasePolygonClas s();
                BasePolygonClas s(int Size);
                virtual ~BasePolygonCla ss();

                const BasePolygonClas s& operator= (const BasePolygonClas s& poly);
                void Copy(BasePolygo nClass * pCopy);

                std::vector<Poi ntPropClass> Points;
                double Area;
                double Length;
                double Height;
                BOOL IsConvex;
                double Angle;
                BOOL XFlip;
                BOOL YFlip;
                };

                class PointPropClass
                {
                public:
                PointPropClass( );
                virtual ~PointPropClass ();
                const PointPropClass& operator= (const PointPropClass& poly);
                void Copy(PointPropC lass * pCopy);

                BOOL TP;
                double Angle;
                int Num;
                int CavNum;
                int St;
                int Fin;
                int Type;
                BOOL Neg;
                BOOL IsGhosh;
                double x;
                double y;
                };


                Comment

                • Chris Theis

                  #9
                  Re: std::vector question


                  "Hamish" <h.dean@xtra.co .nz> wrote in message
                  news:PULAd.3637 $mo2.201785@new s.xtra.co.nz...[color=blue][color=green][color=darkred]
                  > > > I havea program which on execution gives unpredictable behaviour (it
                  > > > shouldn't). In trying to track down the problem, I'm wondering if[/color][/color][/color]
                  there[color=blue]
                  > is[color=green]
                  > > a[color=darkred]
                  > > > difference between these two ways of filling a std::vector with data:
                  > > >
                  > > > Method 1:
                  > > >
                  > > > std::vector<int > v;
                  > > > int k;
                  > > >
                  > > > for(i=0;i<n;i++ ){
                  > > > k = i + 3;
                  > > > v.push_back(k);
                  > > > }
                  > > >
                  > > > Method 2:
                  > > >
                  > > > std::vector<int > v;
                  > > >
                  > > > for(i=0;i<n;i++ ){
                  > > > int k = i + 3;
                  > > > v.push_back(k);
                  > > > }
                  > > >[/color]
                  > >
                  > > The only difference with these two methods is the scope of k but this
                  > > doesn´t affect the behavior in this case. Please post your real code[/color][/color]
                  which[color=blue][color=green]
                  > > gives you trouble.[/color]
                  >
                  > I haven't been able to track down the problem yet. However, the only
                  > difference between an older version which worked, adn this version is the
                  > following data structures:
                  >
                  > std::vector<Pol yTypeClass*> PolyTypes;
                  >
                  > class PolyTypeClass{
                  > public:
                  > PolyTypeClass() ;
                  > virtual ~PolyTypeClass( );
                  >
                  > const PolyTypeClass& operator= (const PolyTypeClass& poly);
                  > void Copy(PolyTypeCl ass * pCopy);
                  >
                  > int ID;
                  > std::vector<Bas ePolygonClass> Rot;
                  > };
                  >
                  > class BasePolygonClas s{
                  > public:
                  > BasePolygonClas s();
                  > BasePolygonClas s(int Size);
                  > virtual ~BasePolygonCla ss();
                  >
                  > const BasePolygonClas s& operator= (const BasePolygonClas s& poly);
                  > void Copy(BasePolygo nClass * pCopy);
                  >
                  > std::vector<Poi ntPropClass> Points;
                  > double Area;
                  > double Length;
                  > double Height;
                  > BOOL IsConvex;
                  > double Angle;
                  > BOOL XFlip;
                  > BOOL YFlip;
                  > };
                  >
                  > class PointPropClass
                  > {
                  > public:
                  > PointPropClass( );
                  > virtual ~PointPropClass ();
                  > const PointPropClass& operator= (const PointPropClass& poly);
                  > void Copy(PointPropC lass * pCopy);
                  >
                  > BOOL TP;
                  > double Angle;
                  > int Num;
                  > int CavNum;
                  > int St;
                  > int Fin;
                  > int Type;
                  > BOOL Neg;
                  > BOOL IsGhosh;
                  > double x;
                  > double y;
                  > };
                  >[/color]

                  What strikes me (although in this case it should not pose a problem after a
                  quick glance at your code) is that you do supply a dtor and an assignment op
                  but not a copy ctor. If you really need a dtor & an assignment op it´s good
                  practice to supply a copy ctor too.

                  If you could please post the actual code of "Method 1" & "Method 2" which
                  works with your data structures and also the implementation of the Copy()
                  method would be interesting.

                  Cheers
                  Chris


                  Comment

                  Working...