0

Possible Duplicate:
Dynamically Growing an Array in C++

I want to create a loop (do while) and create there until a specidfic case(while) objects of a class. Is it possible not to define the number of the objects(for example in an array of objects). I thought to store them in a txt. However is there alternative just with the use of memory??

Community
  • 1
  • 1
snake plissken
  • 2,489
  • 9
  • 37
  • 61
  • Duplicate of http://stackoverflow.com/questions/2261440/dynamic-allocation-of-memory and http://stackoverflow.com/questions/2717199/dynamically-created-arrays and http://stackoverflow.com/questions/3903169/dynamically-growing-an-array-in-c and so many others. – Polynomial Nov 15 '11 at 16:38
  • Look up `std::vector<>`. It's the easiest way to store a variable number of objects. – David Thornley Nov 15 '11 at 16:39
  • If you want to store them in a text file during the loop iteration, you could just stick with temporary objects? – moooeeeep Nov 15 '11 at 16:43

1 Answers1

3

Use std::vector<> and you don't need to handle the allocation at all. Just iterate and add to the vector when you need to. It will grow dynamically to accommodate its contents.

David Heffernan
  • 572,264
  • 40
  • 974
  • 1,389