Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. vector assignment operations

vector assignment operations

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicsquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    misha_grewal
    wrote on last edited by
    #1

    Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)

    S N J 3 Replies Last reply
    0
    • M misha_grewal

      Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      misha_grewal wrote:

      What happens to the objects contained in pVect when the assignment operator is called?

      There aren't any objects in pVect when the assignment operator is called. The best code to use would probably be

      pVect = new std::vector<myclass>(vect.begin(), vect.end());
      

      This allocates the new vector and copies the contents of vect in one operation.

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        misha_grewal wrote:

        What happens to the objects contained in pVect when the assignment operator is called?

        There aren't any objects in pVect when the assignment operator is called. The best code to use would probably be

        pVect = new std::vector<myclass>(vect.begin(), vect.end());
        

        This allocates the new vector and copies the contents of vect in one operation.

        M Offline
        M Offline
        misha_grewal
        wrote on last edited by
        #3

        Thanks a lot Stuart, I have one more doubt..

        Stuart Dootson wrote:

        What happens to the objects contained in pVect when the assignment operator is called? There aren't any objects in pVect when the assignment operator is called.

        But what if pVect already has some objects for e.g. *pVect = vect; //(this time vect has 10 objects) //Again at some point in code *pVect = vect; //(this time vect has 5 objects) Regards, Misha

        S 1 Reply Last reply
        0
        • M misha_grewal

          Thanks a lot Stuart, I have one more doubt..

          Stuart Dootson wrote:

          What happens to the objects contained in pVect when the assignment operator is called? There aren't any objects in pVect when the assignment operator is called.

          But what if pVect already has some objects for e.g. *pVect = vect; //(this time vect has 10 objects) //Again at some point in code *pVect = vect; //(this time vect has 5 objects) Regards, Misha

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          The objects will be destructed, so for

          std::vector<myclass> a;
          

          the myclass destructor will be called for each member of a if you assign something else to a

          1 Reply Last reply
          0
          • M misha_grewal

            Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            misha_grewal wrote:

            std::vector *pVect = new std::vector;

            STL containers are generally designed with stack semantics in mind. Of course, it is allowed to create them on the heap, but it is rarely a good thing.


            Programming Blog utf8-cpp

            1 Reply Last reply
            0
            • M misha_grewal

              Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              I am confused! :confused: Q1) Why are you using new at all? :doh:

              misha_grewal wrote:

              3. *pVect = vect;

              The objects in pVect are destroyed and then replaced by copies of those stored in vect; the vector will resize its self if needed.

              misha_grewal wrote:

              Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)

              In this scenario you are wasting time and code: 1) You do not need to use if(pVect) in C++, because delete does that for you. So you would just call delete. 2) See Q1 above. Note: Using new is rarely required in a well written (standard) C++ program.

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups