Vectors are to store names like string, int, size, and etc.. While arrays store numbers am I right. |
Not really, no.
Let's backtrack a bit:
A
variable is like a box that can hold some information. What
type of information a variable can contain depends on whatever type you give it. For some basic examples, a
char
can hold a single character, an
int
can hold a numeric integer, a
float
can hold a floating point number, and a
string
can hold a textual string.
An
array is simply a group of individual variables. IE, if you wanted 5 integers, you could do this:
1 2 3 4 5
|
int v0; // create 5 integers, each with its own name
int v1; // each variable can hold its own number
int v2;
int v3;
int v4;
|
Or, you could use an array:
1 2 3
|
int v[5]; // <- this creates 5 integers, each accessed with the same name.
// ie, v[0] would be the first of the 5, and v[4] would be the last of the 5.
// As with the above, each individual element in this array can hold its own number.
|
vector
is basically exactly the same as an array. The big difference is that once you create an array at a specific size, it has to keep that size forever. Example:
int v[5];
will always have 5 integers -- it can never have more or less.
On the other hand, vectors can be resized whenever you want. Individual elements can be added/removed to it whenever.
So:
1 2 3
|
std::vector<int> v(5); // <- an array of 5 ints. Same as before... v[0] is the first, v[4] is the last
v.resize(10); // <- now it's an array of 10 ints. v[9] is now the last one
|