Dynamic data in c++
I'm coding in C++ and I'm in need for a dynamic data storage, like ArrayList in C# or Java.
Can anyone help m开发者_如何学Pythone with that? I'm not sure what to use. Thanks!
std::vector is what you're looking for.
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.
std::vector
is your friend, here is a tutorial.
or std::list
for that matter...
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.
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
精彩评论