How to make a dynamic storage of objects (c++)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am a beginner to programming and I am trying to find a way to create a dynamic storage of objects of my pigeon class. Here is my code:



class pigeon {
public:
pigeon(std::string nameI);
void outputInfo();
private:
std::string name;
};


The idea is that I want to be able to add a new object, have a place to store its information, then be able to add another object, and so on. I have no idea where to start with this or even what data structure to use, I have no experience storing objects.










share|improve this question


















  • 4





    Are the objects you want to store of the same type? If so you can simply use a std::vector<YourObject>

    – Zpalmtree
    Nov 21 '18 at 22:14






  • 4





    std::vector

    – François Andrieux
    Nov 21 '18 at 22:15











  • You need to indicate in your question what you mean by store. Do you mean in memory or something more permanent?

    – CRAGIN
    Nov 21 '18 at 22:17











  • As @FrançoisAndrieux already said. Use a std::vector.

    – Jesper Juhl
    Nov 21 '18 at 22:37




















0















I am a beginner to programming and I am trying to find a way to create a dynamic storage of objects of my pigeon class. Here is my code:



class pigeon {
public:
pigeon(std::string nameI);
void outputInfo();
private:
std::string name;
};


The idea is that I want to be able to add a new object, have a place to store its information, then be able to add another object, and so on. I have no idea where to start with this or even what data structure to use, I have no experience storing objects.










share|improve this question


















  • 4





    Are the objects you want to store of the same type? If so you can simply use a std::vector<YourObject>

    – Zpalmtree
    Nov 21 '18 at 22:14






  • 4





    std::vector

    – François Andrieux
    Nov 21 '18 at 22:15











  • You need to indicate in your question what you mean by store. Do you mean in memory or something more permanent?

    – CRAGIN
    Nov 21 '18 at 22:17











  • As @FrançoisAndrieux already said. Use a std::vector.

    – Jesper Juhl
    Nov 21 '18 at 22:37
















0












0








0








I am a beginner to programming and I am trying to find a way to create a dynamic storage of objects of my pigeon class. Here is my code:



class pigeon {
public:
pigeon(std::string nameI);
void outputInfo();
private:
std::string name;
};


The idea is that I want to be able to add a new object, have a place to store its information, then be able to add another object, and so on. I have no idea where to start with this or even what data structure to use, I have no experience storing objects.










share|improve this question














I am a beginner to programming and I am trying to find a way to create a dynamic storage of objects of my pigeon class. Here is my code:



class pigeon {
public:
pigeon(std::string nameI);
void outputInfo();
private:
std::string name;
};


The idea is that I want to be able to add a new object, have a place to store its information, then be able to add another object, and so on. I have no idea where to start with this or even what data structure to use, I have no experience storing objects.







c++ data-structures






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 22:13









NipIsTrueNipIsTrue

176




176








  • 4





    Are the objects you want to store of the same type? If so you can simply use a std::vector<YourObject>

    – Zpalmtree
    Nov 21 '18 at 22:14






  • 4





    std::vector

    – François Andrieux
    Nov 21 '18 at 22:15











  • You need to indicate in your question what you mean by store. Do you mean in memory or something more permanent?

    – CRAGIN
    Nov 21 '18 at 22:17











  • As @FrançoisAndrieux already said. Use a std::vector.

    – Jesper Juhl
    Nov 21 '18 at 22:37
















  • 4





    Are the objects you want to store of the same type? If so you can simply use a std::vector<YourObject>

    – Zpalmtree
    Nov 21 '18 at 22:14






  • 4





    std::vector

    – François Andrieux
    Nov 21 '18 at 22:15











  • You need to indicate in your question what you mean by store. Do you mean in memory or something more permanent?

    – CRAGIN
    Nov 21 '18 at 22:17











  • As @FrançoisAndrieux already said. Use a std::vector.

    – Jesper Juhl
    Nov 21 '18 at 22:37










4




4





Are the objects you want to store of the same type? If so you can simply use a std::vector<YourObject>

– Zpalmtree
Nov 21 '18 at 22:14





Are the objects you want to store of the same type? If so you can simply use a std::vector<YourObject>

– Zpalmtree
Nov 21 '18 at 22:14




4




4





std::vector

– François Andrieux
Nov 21 '18 at 22:15





std::vector

– François Andrieux
Nov 21 '18 at 22:15













You need to indicate in your question what you mean by store. Do you mean in memory or something more permanent?

– CRAGIN
Nov 21 '18 at 22:17





You need to indicate in your question what you mean by store. Do you mean in memory or something more permanent?

– CRAGIN
Nov 21 '18 at 22:17













As @FrançoisAndrieux already said. Use a std::vector.

– Jesper Juhl
Nov 21 '18 at 22:37







As @FrançoisAndrieux already said. Use a std::vector.

– Jesper Juhl
Nov 21 '18 at 22:37














2 Answers
2






active

oldest

votes


















0














Make vector with pointer of your class:



std::vector<pigeon*> pigeons;


Then allocate new pigeon object and push it into your vector:



pigeon * pig = new pigeon("pigeon");
pigeons.push_back(pig);





share|improve this answer



















  • 1





    Is there a reason you suggest using raw owning pointers and adding another level of indirection where std::vector<pigeon> already does what the OP wants?

    – Swordfish
    Nov 21 '18 at 23:28













  • Op wants to store dynamic objects. When you use vector<pigeon> it first copies the object and stores it. This is not dynamic storing. And some point of view it is not efficient.

    – kara
    Nov 21 '18 at 23:39






  • 1





    There is always emplace_back() and smart pointers.

    – Swordfish
    Nov 21 '18 at 23:42













  • Can you give an example of usage of these?

    – kara
    Nov 21 '18 at 23:46






  • 2





    If copying objects is very expensive, don't copy. There are better ways like having a collection of indexes around that you reorder instead of the objects in the vector. Maybe use a std::list<std::unique_ptr<pigeon>>.

    – Swordfish
    Nov 22 '18 at 0:30



















0














As it was already pointed out in the comments, you should preferably use a container that handles its resources following the RAII/RDID-idiom ( "Resource Acquisition Is Initialisation" / "Resource Destruction is Deletion") so you don't have to worry about it yourself. This is also a simple way of preventing resource leaks when an exception is thrown.



One of the commonly used containers of the C++ standard library is std::vector<>.



You'd use it like this (just to give you an initial idea, please refer to the documentation for further explanation and examples):



#include <vector>

// ...

{
std::vector<pigeon> pigeons;

pigeons.push_back("Karl"); // add three pigeons
pigeons.push_back("Franz"); // at the end of the
pigeons.push_back("Xaver"); // vector

pigeons[1]; // access "Franz"

for(auto /* maybe const */ &p : pigeons) { // iterate over the vector
// do something with pigeon p
}

} // pigeons goes out of scope, its destructor is called which
// takes care of deallocating the memory used by the vector.





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421184%2fhow-to-make-a-dynamic-storage-of-objects-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Make vector with pointer of your class:



    std::vector<pigeon*> pigeons;


    Then allocate new pigeon object and push it into your vector:



    pigeon * pig = new pigeon("pigeon");
    pigeons.push_back(pig);





    share|improve this answer



















    • 1





      Is there a reason you suggest using raw owning pointers and adding another level of indirection where std::vector<pigeon> already does what the OP wants?

      – Swordfish
      Nov 21 '18 at 23:28













    • Op wants to store dynamic objects. When you use vector<pigeon> it first copies the object and stores it. This is not dynamic storing. And some point of view it is not efficient.

      – kara
      Nov 21 '18 at 23:39






    • 1





      There is always emplace_back() and smart pointers.

      – Swordfish
      Nov 21 '18 at 23:42













    • Can you give an example of usage of these?

      – kara
      Nov 21 '18 at 23:46






    • 2





      If copying objects is very expensive, don't copy. There are better ways like having a collection of indexes around that you reorder instead of the objects in the vector. Maybe use a std::list<std::unique_ptr<pigeon>>.

      – Swordfish
      Nov 22 '18 at 0:30
















    0














    Make vector with pointer of your class:



    std::vector<pigeon*> pigeons;


    Then allocate new pigeon object and push it into your vector:



    pigeon * pig = new pigeon("pigeon");
    pigeons.push_back(pig);





    share|improve this answer



















    • 1





      Is there a reason you suggest using raw owning pointers and adding another level of indirection where std::vector<pigeon> already does what the OP wants?

      – Swordfish
      Nov 21 '18 at 23:28













    • Op wants to store dynamic objects. When you use vector<pigeon> it first copies the object and stores it. This is not dynamic storing. And some point of view it is not efficient.

      – kara
      Nov 21 '18 at 23:39






    • 1





      There is always emplace_back() and smart pointers.

      – Swordfish
      Nov 21 '18 at 23:42













    • Can you give an example of usage of these?

      – kara
      Nov 21 '18 at 23:46






    • 2





      If copying objects is very expensive, don't copy. There are better ways like having a collection of indexes around that you reorder instead of the objects in the vector. Maybe use a std::list<std::unique_ptr<pigeon>>.

      – Swordfish
      Nov 22 '18 at 0:30














    0












    0








    0







    Make vector with pointer of your class:



    std::vector<pigeon*> pigeons;


    Then allocate new pigeon object and push it into your vector:



    pigeon * pig = new pigeon("pigeon");
    pigeons.push_back(pig);





    share|improve this answer













    Make vector with pointer of your class:



    std::vector<pigeon*> pigeons;


    Then allocate new pigeon object and push it into your vector:



    pigeon * pig = new pigeon("pigeon");
    pigeons.push_back(pig);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 22:52









    karakara

    576




    576








    • 1





      Is there a reason you suggest using raw owning pointers and adding another level of indirection where std::vector<pigeon> already does what the OP wants?

      – Swordfish
      Nov 21 '18 at 23:28













    • Op wants to store dynamic objects. When you use vector<pigeon> it first copies the object and stores it. This is not dynamic storing. And some point of view it is not efficient.

      – kara
      Nov 21 '18 at 23:39






    • 1





      There is always emplace_back() and smart pointers.

      – Swordfish
      Nov 21 '18 at 23:42













    • Can you give an example of usage of these?

      – kara
      Nov 21 '18 at 23:46






    • 2





      If copying objects is very expensive, don't copy. There are better ways like having a collection of indexes around that you reorder instead of the objects in the vector. Maybe use a std::list<std::unique_ptr<pigeon>>.

      – Swordfish
      Nov 22 '18 at 0:30














    • 1





      Is there a reason you suggest using raw owning pointers and adding another level of indirection where std::vector<pigeon> already does what the OP wants?

      – Swordfish
      Nov 21 '18 at 23:28













    • Op wants to store dynamic objects. When you use vector<pigeon> it first copies the object and stores it. This is not dynamic storing. And some point of view it is not efficient.

      – kara
      Nov 21 '18 at 23:39






    • 1





      There is always emplace_back() and smart pointers.

      – Swordfish
      Nov 21 '18 at 23:42













    • Can you give an example of usage of these?

      – kara
      Nov 21 '18 at 23:46






    • 2





      If copying objects is very expensive, don't copy. There are better ways like having a collection of indexes around that you reorder instead of the objects in the vector. Maybe use a std::list<std::unique_ptr<pigeon>>.

      – Swordfish
      Nov 22 '18 at 0:30








    1




    1





    Is there a reason you suggest using raw owning pointers and adding another level of indirection where std::vector<pigeon> already does what the OP wants?

    – Swordfish
    Nov 21 '18 at 23:28







    Is there a reason you suggest using raw owning pointers and adding another level of indirection where std::vector<pigeon> already does what the OP wants?

    – Swordfish
    Nov 21 '18 at 23:28















    Op wants to store dynamic objects. When you use vector<pigeon> it first copies the object and stores it. This is not dynamic storing. And some point of view it is not efficient.

    – kara
    Nov 21 '18 at 23:39





    Op wants to store dynamic objects. When you use vector<pigeon> it first copies the object and stores it. This is not dynamic storing. And some point of view it is not efficient.

    – kara
    Nov 21 '18 at 23:39




    1




    1





    There is always emplace_back() and smart pointers.

    – Swordfish
    Nov 21 '18 at 23:42







    There is always emplace_back() and smart pointers.

    – Swordfish
    Nov 21 '18 at 23:42















    Can you give an example of usage of these?

    – kara
    Nov 21 '18 at 23:46





    Can you give an example of usage of these?

    – kara
    Nov 21 '18 at 23:46




    2




    2





    If copying objects is very expensive, don't copy. There are better ways like having a collection of indexes around that you reorder instead of the objects in the vector. Maybe use a std::list<std::unique_ptr<pigeon>>.

    – Swordfish
    Nov 22 '18 at 0:30





    If copying objects is very expensive, don't copy. There are better ways like having a collection of indexes around that you reorder instead of the objects in the vector. Maybe use a std::list<std::unique_ptr<pigeon>>.

    – Swordfish
    Nov 22 '18 at 0:30













    0














    As it was already pointed out in the comments, you should preferably use a container that handles its resources following the RAII/RDID-idiom ( "Resource Acquisition Is Initialisation" / "Resource Destruction is Deletion") so you don't have to worry about it yourself. This is also a simple way of preventing resource leaks when an exception is thrown.



    One of the commonly used containers of the C++ standard library is std::vector<>.



    You'd use it like this (just to give you an initial idea, please refer to the documentation for further explanation and examples):



    #include <vector>

    // ...

    {
    std::vector<pigeon> pigeons;

    pigeons.push_back("Karl"); // add three pigeons
    pigeons.push_back("Franz"); // at the end of the
    pigeons.push_back("Xaver"); // vector

    pigeons[1]; // access "Franz"

    for(auto /* maybe const */ &p : pigeons) { // iterate over the vector
    // do something with pigeon p
    }

    } // pigeons goes out of scope, its destructor is called which
    // takes care of deallocating the memory used by the vector.





    share|improve this answer






























      0














      As it was already pointed out in the comments, you should preferably use a container that handles its resources following the RAII/RDID-idiom ( "Resource Acquisition Is Initialisation" / "Resource Destruction is Deletion") so you don't have to worry about it yourself. This is also a simple way of preventing resource leaks when an exception is thrown.



      One of the commonly used containers of the C++ standard library is std::vector<>.



      You'd use it like this (just to give you an initial idea, please refer to the documentation for further explanation and examples):



      #include <vector>

      // ...

      {
      std::vector<pigeon> pigeons;

      pigeons.push_back("Karl"); // add three pigeons
      pigeons.push_back("Franz"); // at the end of the
      pigeons.push_back("Xaver"); // vector

      pigeons[1]; // access "Franz"

      for(auto /* maybe const */ &p : pigeons) { // iterate over the vector
      // do something with pigeon p
      }

      } // pigeons goes out of scope, its destructor is called which
      // takes care of deallocating the memory used by the vector.





      share|improve this answer




























        0












        0








        0







        As it was already pointed out in the comments, you should preferably use a container that handles its resources following the RAII/RDID-idiom ( "Resource Acquisition Is Initialisation" / "Resource Destruction is Deletion") so you don't have to worry about it yourself. This is also a simple way of preventing resource leaks when an exception is thrown.



        One of the commonly used containers of the C++ standard library is std::vector<>.



        You'd use it like this (just to give you an initial idea, please refer to the documentation for further explanation and examples):



        #include <vector>

        // ...

        {
        std::vector<pigeon> pigeons;

        pigeons.push_back("Karl"); // add three pigeons
        pigeons.push_back("Franz"); // at the end of the
        pigeons.push_back("Xaver"); // vector

        pigeons[1]; // access "Franz"

        for(auto /* maybe const */ &p : pigeons) { // iterate over the vector
        // do something with pigeon p
        }

        } // pigeons goes out of scope, its destructor is called which
        // takes care of deallocating the memory used by the vector.





        share|improve this answer















        As it was already pointed out in the comments, you should preferably use a container that handles its resources following the RAII/RDID-idiom ( "Resource Acquisition Is Initialisation" / "Resource Destruction is Deletion") so you don't have to worry about it yourself. This is also a simple way of preventing resource leaks when an exception is thrown.



        One of the commonly used containers of the C++ standard library is std::vector<>.



        You'd use it like this (just to give you an initial idea, please refer to the documentation for further explanation and examples):



        #include <vector>

        // ...

        {
        std::vector<pigeon> pigeons;

        pigeons.push_back("Karl"); // add three pigeons
        pigeons.push_back("Franz"); // at the end of the
        pigeons.push_back("Xaver"); // vector

        pigeons[1]; // access "Franz"

        for(auto /* maybe const */ &p : pigeons) { // iterate over the vector
        // do something with pigeon p
        }

        } // pigeons goes out of scope, its destructor is called which
        // takes care of deallocating the memory used by the vector.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 23:51

























        answered Nov 21 '18 at 23:39









        SwordfishSwordfish

        10.2k11437




        10.2k11437






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421184%2fhow-to-make-a-dynamic-storage-of-objects-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)