Arrays and Vectors

Can We use vectors instead of arrays? Is Arrays and Vectors same?

1 Like

Hi! :grinning:
In short vectors are basically arrays that can change in size.
I would avoid using arrays, instead I would use vectors as they can do almost the same things but when needed, you can push a value to a vector making it more flexible or remove one without causing an error.

Although in some cases you might want to use arrays, like when you know that you will have a specific number of values. But you should probably use vectors in that case too, if not me, than my bad decision should convince you:
I recently was making a game in c++ when I settled on using arrays because I knew that I will have a constant number of enemies, even when they will die I would have replaced them with a different object, like a new Dead object.
But I changed my mind, about having dead enemies, so I had to rewrite the entire thing to be able to remove enemies, and add new enemies when a game was started or when you would spawn enemies.
Using vectors would have saved me from having to rewrite most of the code.

Vectors are mutable just like a list in python if you are familiar with it. Arrays are not, which complicates things especially when we want to edit do things with a list of items.

Both are similar and easy to use, but for the sake of courses and demonstrating what you can do vectors are usually more useful