Few words about: how useful is using vector in small projects

When I started learning programming, nobody told me that exists very, very useful tool like vector. Somebody could say: ‘Ok Wojtek, but what is it ?’ With pleasure I can explain this term. Vector is an array, but you don’t declare a size. However, array will be increase without any problems when you will add next (for example) integers. If you will be curious of size – you can use: array.size(); I still use vector, almost in every project because I enjoyed seriously. You can use this type of array when you will not know how big will be an array. Let’s go to sample project.

Firstly, when you create a new project, you must add the #include <vector>. I am sure that you know how to declare basic array, but I put below an example and then I will compare an array and a vector.

Capture
As you can see: there is a difference in a record. In normal array you have to define a size, but in a vector – no. Let’s check how to add anything to this type of array.

‘numbers.push_back(random)’ is necessary if you want to fill in vector. Every single integer (in this case) will be located on the end of numbers (push_back). As I wrote (at the beginning of the post) if you want to check size, you should use command ‘numbers.size()’. Let’s add that.

Conclusion: vector is an array which is elastic and it helps during solving ‘matura’ tasks. If you want to know more about this topic, I recommend you these sources:
First source
Second source
Third source

Leave a comment