Just wanted to chime in a bit because you will see a lot on this forum that people are really against arrays here. Though that doesn't mean they are evil or bad to use. On the contrary actually sometimes arrays are a best fit then std::vector<> is.
The main thing you need to know is when to use them and when it would be better to use std::vector<> instead. Like every container it comes down to what your requirements are.
One of the most basic rules for arrays vs vectors is this.
If you need a storage container that is dynamic in nature you are going to want to use std::vector<> over an array. By dynamic in nature I mean that over the lifetime of the container object the number of elements stored in it will change, either by adding new elements or deleting some.
Otherwise if you are 100% certain that the number of elements will not be changing over the lifetime of the container object you should instead use an array. There is various reasons behind why you should do this but they are a bit more advanced topics (Where they are stored in memory, performance concerns, etc.).
I also should say that while I don't personally find C-style arrays evil (As long as they are used correctly) you really should use std::array<> instead of C-style arrays when the situation calls for an array. It will provide you basically all the benefits of a C-style array along with additional functionality that allows you to work more easily with the STL.
But anyways just wanted to point out that while yes most of the time an std::vector<> will probably be the best solution (Like it seems for your case) it is not a catch all container that you always use. It is best to always look at the requirements you have and then choose the best container from them.
I'll start researching vectors because I don't know what '.pushback' means. |
std::vector<>::push_back() is basically just saying that you wish to "push" this object onto the back of the container. Meaning it will resize the container so that the new object can fit in it and then add that element to the back of the container. So for example here is a std::vector<int> example of this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <vector>
#include <iostream>
int main()
{
// Notice how you can use initialization lists with std::vector<> also.
std::vector<int> container = {1, 2, 3};
// Now lets say we want to add a new number to the container, the number 4
// We just call the push_back() method and it will automatically resize the container
// if it is needed and add that number to the back of the container.
container.push_back(4);
// Now container[3] = 4
std::cout << "container[3] = " << container[3] << std::endl;
// Just printing to show all the elements in the container to help
// Uses C++11 code but you can ignore this it is just printing everything in the vector
std::cout << "Printing all elements!" << std::endl;
for (int i : container)
{
std::cout << i << std::endl;
}
return 0;
}
|
So that is a quick demo of what push_back does and also note that you can use initialization lists to create the vector just like you do with arrays.
Hopes this helps clarify some things and sorry bout such a long post it is a bad habit of mine ;p.