Hi there!
I'm coding in C++ and I'm in need for a dynamic data storage, like ArrayList in C# or Java.

Can anyone help me with that? I'm not sure what to use. Thanks!

+4  A: 

std::vector is your friend, here is a tutorial.

+8  A: 

You are looking for std::vector. You can read here about it (scroll down on that page to view a description of its functions).

Vectors have constant-time lookup. Insertion/removal is fast at the end of a vector, but (as the link I posted explains in more detail) is slower otherwise. Additionally, vectors have to be resized as you store additional data in them, so it is worth looking into reserve (this is like ArrayLists' ensureCapacity). Note that this resizing is automatic - reserve is there only for performance reasons.

+11  A: 

std::vector is what you're looking for.

Hamid Nazari
+1  A: 

or std::list for that matter...

If you want an alternative, `std::deque` is probably the second choice for simple dynamic storage. A linked list is the container of choice in only a few, mostly rather unusual, situations. See: http://stackoverflow.com/questions/2429217/under-what-circumstances-are-linked-lists-useful
Jerry Coffin

If you're really just a beginner, you should start with the basics: arrays.

Once you understand what's going on underneath, then you should move on to the STL and containers (like vector) like everyone else is suggesting.

Austin Hyde
@Austin: I'd say just the opposite: start with vector. At some point in the (probably distant) future, you might have a reason to use an array, but for beginners (and most advanced programmers, for that matter) they're best avoided.
Jerry Coffin
@Austin: I think that this is definitely up for debate. To understand arrays dynamically (and use them safely), it is necessary to understand manual management of memory on a very practical level (pointers, new, delete, referencing, dereferencing, etc..). One can explain how vectors work with more general/simple concepts, which give a nice introduction to memory management. I would guess it is for this reason that several of the highly recommended C++ texts (Accelerated C++ and Stroustrup's Programming: Principles and Practice) introduce std::vector long before arrays.
@Jerry, MarkD: I stand corrected then. In my Comp Sci classes, we were taught arrays, then pointers, then templates, then *barely* touched on STL. The thinking was that once you understood the foundations, you could better understand how to use and work the higher level library. In retrospect though, I really don't agree with a lot that they taught us: my professor *insisted* that I put a `ostream }`.
Austin Hyde

Well there are 2 options that pop out right away.

The first is to use std::vector. A vector works basically the same as an array (except for declaration and calling syntax) on the surface. The 2 things you want to know about vectors is that you can call a function that will increase the size of your vector (add indexes onto the end). The other thing is that to create a 2 dimensional vector you would need to declare a vector of vectors. This also allows you to have more than 2 dimentions.

The other thing you can use is std:: list. This is just a linked list into which you can insert and delete items.


They're both undoubtedly mentioned in the book list, but right about now, it sounds like you need one (or both) of the following:

The C++ Standard Library, by Nicolai Josuttis.
STL Tutorial and Reference Guide, 2nded., by Musser, Saini, and Derge

Jerry Coffin