When to use arrays or vectors

In what seniors is using arrays better than vectors? I’m pretty sure its best to use arrays when possible, but when the problem requires you to get indexes of values in the array, you would have to create a seperate function or a loop which is annoying, so would using vectors be better?

1D vectors are usually about as fast as arrays, so I generally use vectors.

1 Like

Thanks! BTW, when iterating through a data structure, should the pecking order be:
{
Use for-each loop when possible
Index loop if the data structure allows it (arrays, vectors, but maps don’t support this)
Iterator-based loop
}

there is no pecking order, write what will be easiest and what gets the job done
sometimes u just want the value and a for each loop is short and concise
sometimes u want to know the index and u have to iterate over index
sometimes the situation calls for iterators and it will be messier

as u practice more you will be able to see what works for you in what situation :] dont worry too much about it now

1 Like