Vectors are essentially arrays that automatically grow and shrink.
Vectors are good for various things. If you are writing code, and you have to have a variable amount of memory, vectors are a quick fix. When you add elements to the vector, if it needs to expand the memory, it will. When you remove elements, if you get below a specific percent, the vector will shrink. A lot of the implementations of vectors also have various debugging features, to prevent you from doing bad things (removing to much data, accessing a bad index, ect...).
While all of the above may sound nice, there are some catches. First, there is a lot of overhead (function call overhead, addition memory, error checking). IE, a vector is a lot slower then a array. Other then that, vectors are compiler specific. The C++ standard only specifics the complexity of each vector function. So, while one compilers vector implementation may be fast, another may be really slow.
So, to cap it up
Vector
Pros
- Array Based
- Dynamic
- Error checking (compiler specific, so there might not be any)
Cons
- A lot of overhead/Slow (compared to a array)
- Complier specific implementations
QUOTE
if vectors, then with iterator or without it ?
This is more of a personal opinion. I personally prefer to just use indexes, since its a lot less typing, a lot more array like, and a lot more memory efficient. The differences of access the data via a index and a iterator is going to be compiler specific, so I can't really go into the specifics.
So, to cap everything up.
You should use a vector when
- You need a variable sized of array, and want to write minimal code
You should use a array when
- You need the fastest possible code
- You don't need a dynamically sized array