Hi, I'm an amateur C++ user, and I have a pretty good knowledge of all the basics, but I'm a little confused about vectors... I've read the tutorial article on them, and I didn't quite understand (I'm very beginner) how exactly do you use vectors, and how do you use them efficiently?
what I'd like to know is when would it be a good time to use a vector?
Anywhere where you need a collection of objects, when they need to be in a specific order, and when you're likely to be adding elements to the end of the sequence but not inserting them into the middle.
Generally speaking, you can start by using them in places where you're currently using arrays.
Edit: Yeah, this is a gross simplification, but it's a good starting point for a beginner.
When ever you will be dynamically removing and adding elements. If you are going to have the same elements every time or even same size then you should just use an array. I prefer stl containers over the [] arrays.
arrays are only used if you have a static size ex int arr[ 3 ] = { 1 , 2 , 3 };
where as a vector the size can change when ever you can add or remove elements.
That's not really true - you can dynamically allocate an array using new.
In any case, it's almost always better to use a vector rather than an array, since vectors handle their own memory management, and contain lots of useful methods on them. The only times its really worth using a C-style array is when you desperately need to eke out the last bit of performance or memory efficiency - something which is rarely the case.
It depends on what you mean when you say "replace arrays".
Basically, a vector is an array of variable size. Like said previously but worth noting again, vectors are useful when you have have need of array-like functionality but may need to change the size of the array. To do this with normal arrays, you would need to allocate a new array of the new size and move all of the elements to it. Instead, you can use a vector where its storage capacity is handled automatically by the container.
*EDIT: Well, slow and steady doesn't always end up 'winning' the race... Sorry for the late post.