7

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!

Ricardo
  • 11,609
  • 8
  • 27
  • 39
  • 8
    [Time to get a good book.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) :) – GManNickG Jul 22 '10 at 17:52

7 Answers7

11

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

Hamid Nazari
  • 3,905
  • 2
  • 28
  • 31
8

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.

Cam
  • 14,930
  • 16
  • 77
  • 128
4

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

Justicle
  • 14,761
  • 17
  • 70
  • 94
1

or std::list for that matter...

anirvan
  • 4,797
  • 4
  • 32
  • 42
  • 1
    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 Jul 22 '10 at 17:56
0

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
  • 26,347
  • 28
  • 96
  • 129
  • 2
    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 Jul 22 '10 at 17:53
  • 2
    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. – MarkD Jul 22 '10 at 17:59
  • @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& display(ostream& out) const` method on our static-array `Vector` class, as well as a `friend ostream& operator <<(ostream& out, const Type& x) { return x.display(out); }`. – Austin Hyde Jul 22 '10 at 20:12
0

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.

njvb
  • 1,377
  • 3
  • 18
  • 36
0

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
  • 476,176
  • 80
  • 629
  • 1,111