Good enough examples for Vectors and Arrays

Ok so I think im beginning to understand vectors and arrays. I always quite understood them ,but im just making sure. So to put this in the most simplest terms possible. Vectors are to store names like string, int, size, and etc.. While arrays store numbers am I right. Also please include some examples I think some good examples are.
Arrays:
Can be used for keeping track of numbers and hold them. As of a rpg game and your health bar the array can constantly store add and subtract your life.
Vector:
Can be used for things like what im reading in Accelerated C++ like storing a whole value of grades and keeping it. best used for while (cin >> x).

Please tell me if im right also give me some examples
Vectors are automaticaly managed, dynamicly growing safe arrays with a bunch of additional functions. There is nothing you can do with arrays you cannot do with vectors.

And vice versa: many things you can do with vectors, you can do with arrays:

1
2
3
4
std::string strings[] = {"World", "ABC", "Hello"}; //create array of strings
std::sort( std::begin(strings), std::end(strings) ); //Sort them
for(const auto& s: strings) //Output each element
    std::cout << s << '\n';
ABC
Hello
World


Basically in C++ default container should be vector. You can replace it with something else if you will really need it.
Last edited on
Thanks buddy I fully understand them now WOOT!
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 
Last edited on
Topic archived. No new replies allowed.