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. C / C++ / MFC
  4. Moving a STL container around

Moving a STL container around

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsdocker
7 Posts 3 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    A STL container is not your typical c++ class, how do you declare a vector pointer and pass it to a function.

    class someclass
    {
    public:
    int ID;
    }
    void somefunction(vector* vec)
    {
    someclass scitem;

    vec->push_back(scitem);

    }
    vector* vec;
    somefunction(vec);

    I`m also not sure how a vector of pointers would work.

    void somefunction(vector* vec)
    {
    someclass * scitem;
    scitem = (someclass*)malloc(sizeof(someclass));

    vec->push_back(scitem);

    }
    vector* vec2;
    somefuntion(vec2);

    L 1 Reply Last reply
    0
    • C Calin Negru

      A STL container is not your typical c++ class, how do you declare a vector pointer and pass it to a function.

      class someclass
      {
      public:
      int ID;
      }
      void somefunction(vector* vec)
      {
      someclass scitem;

      vec->push_back(scitem);

      }
      vector* vec;
      somefunction(vec);

      I`m also not sure how a vector of pointers would work.

      void somefunction(vector* vec)
      {
      someclass * scitem;
      scitem = (someclass*)malloc(sizeof(someclass));

      vec->push_back(scitem);

      }
      vector* vec2;
      somefuntion(vec2);

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes, STL containers are just template classes and can be manipulated the same as any class objects. And as such, a vector may contain objects, or pointers to objects. But remember in the second case that the items pointed to, must not be removed while the vector still owns them.

      std::vector* pMyvec = new std::vector(); // a pointer to a vector of pointers.
      // use push_back to fill the vector with pointers:
      pMyvec->push_back("A string");
      pMyvec->push_back("Another string");

      // pass the vectro to a function:
      int result = myfun(pMyvec);
      // on return the contents may have been changed

      BTW you should not use malloc in C++ code, as new and delete is the correct way to manage memory.

      C 1 Reply Last reply
      0
      • L Lost User

        Yes, STL containers are just template classes and can be manipulated the same as any class objects. And as such, a vector may contain objects, or pointers to objects. But remember in the second case that the items pointed to, must not be removed while the vector still owns them.

        std::vector* pMyvec = new std::vector(); // a pointer to a vector of pointers.
        // use push_back to fill the vector with pointers:
        pMyvec->push_back("A string");
        pMyvec->push_back("Another string");

        // pass the vectro to a function:
        int result = myfun(pMyvec);
        // on return the contents may have been changed

        BTW you should not use malloc in C++ code, as new and delete is the correct way to manage memory.

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #3

        Quote:

        And as such, a vector may contain objects, or pointers to objects

        But when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added. A linked list deals with pointers not objects.

        Greg UtasG L 2 Replies Last reply
        0
        • C Calin Negru

          Quote:

          And as such, a vector may contain objects, or pointers to objects

          But when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added. A linked list deals with pointers not objects.

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #4

          If you add an object to an STL container (vector, list, set, map...), the object is copied into the container. The objects must all be of the same type, however. The only way to put polymorphic objects into a container is to use a container of pointers to their common base class, in which case the objects need to survive outside the container, probably by having been allocated from the heap.

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          C 1 Reply Last reply
          0
          • Greg UtasG Greg Utas

            If you add an object to an STL container (vector, list, set, map...), the object is copied into the container. The objects must all be of the same type, however. The only way to put polymorphic objects into a container is to use a container of pointers to their common base class, in which case the objects need to survive outside the container, probably by having been allocated from the heap.

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #5

            thanks Greg, I`ll keep that in mind.

            1 Reply Last reply
            0
            • C Calin Negru

              Quote:

              And as such, a vector may contain objects, or pointers to objects

              But when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added. A linked list deals with pointers not objects.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              CalinNegru wrote:

              when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added.

              No, the vector copies whatever type you have declared it with. For example:

              std::vector myVec;
              std::string newstr = "A string";
              myVec.push_back(newstr); // myVec now contains a copy of the string, not a pointer.

              CalinNegru wrote:

              A linked list deals with pointers not objects.

              Well sometimes it does; but what has that to do with this question?

              C 1 Reply Last reply
              0
              • L Lost User

                CalinNegru wrote:

                when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added.

                No, the vector copies whatever type you have declared it with. For example:

                std::vector myVec;
                std::string newstr = "A string";
                myVec.push_back(newstr); // myVec now contains a copy of the string, not a pointer.

                CalinNegru wrote:

                A linked list deals with pointers not objects.

                Well sometimes it does; but what has that to do with this question?

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #7

                ok, I think I get your point

                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