# Vectors

As hinted when going over arrays, vectors are just like arrays, but a bit more powerful. To use vectors, be sure to type “`#include <vector>`” at the top of your program, since they can’t be used by default. Similarly to `std::cout`, vectors also require you to type an “`std::`” prefix (since they are a part of the C++ Standard Library). The program below continues our example of storing a group of ages together, this time using a vector of ages:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
ages = 20;
std::cout << ages << "\n";
return 0;
}
```
##### Creating Vectors

Just like creating other variables, we begin with the data type when creating a vector. In this case, the data type is `std::vector<int>`, which you can think of as meaning “a vector containing integers.” Everything else is the same as an array, except you don’t type square brackets after the vector’s name.

##### Getting and Modifying Elements

You can also see from above that modifying a vector’s elements and getting a vector’s elements is the same as for an array (just include the index in square brackets).

##### Creating Empty Vectors

To create an empty vector with a length of 0, you can just type:

```std::vector<int> vectorNameHere;
```

To create a vector of a certain length, with every element set to 0 by default, just put the length of the vector in parentheses:

```std::vector<int> vectorNameHere(10);
```

Note that this differs from arrays because you don’t need to type “` = {0};`” to set each element equal to 0.

### The Benefits of Vectors

Now, so far we’ve gone through everything that can be done with both vectors and arrays. What actually makes vectors useful?

##### Getting a Vector’s Length

You can easily get the length of a vector (the number of elements inside of it) by typing “`.size()`” after the vector’s name. The reason for the two parentheses is because `size` is a C++ “function.” You can learn more about this in the Functions page of this guide.

Getting a vector’s size can be really useful if we ever need to loop through all of its values, like so:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
for (int i = 0; i < ages.size(); i++) {
std::cout << ages[i] << "\n";
}
return 0;
}
```

For an array, we would need to manually type its length (so, we’d have to write “`4`” rather than “`ages.size()`“) because there’s no simple way of getting that information.

(However, as a side note, it still is possible. If we have an array called “`array`“, its length can be found by doing `sizeof(array) / sizeof(array)`. This works by finding the number of bytes of data within the entire array and dividing that by the number of bytes in a single element, which gets the total number of elements. You shouldn’t have to use this very often, though.)

##### Modifying All of a Vector’s Elements

Unlike with arrays, you can set a vector equal to an entire new list of elements, like this:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
ages = {6, 15, 10, 11};
for (int i = 0; i < ages.size(); i++) {
std::cout << ages[i] << "\n";
}
return 0;
}
```
##### Adding an Element to the End of a Vector

We can use “`.push_back(elementGoesHere)`” to increase the length of a vector by 1 and add an element to the end of the vector:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
ages.push_back(13);
ages.push_back(9);
for (int i = 0; i < ages.size(); i++) {
std::cout << ages[i] << "\n";
}
return 0;
}
```
##### Removing an Element from the End of a Vector

In a similar fashion, “`.pop_back()`” removes the last element of a vector and decreases the vector’s size by 1:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
ages.pop_back();
ages.pop_back();
for (int i = 0; i < ages.size(); i++) {
std::cout << ages[i] << "\n";
}
return 0;
}
```
##### Inserting an Element Anywhere Inside a Vector

By using “`.insert(iteratorGoesHere, elementGoesHere)`“, we can insert an element at any index, which causes all following elements to have their indices increased by 1:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
ages.insert(ages.begin() + 2, 8);
for (int i = 0; i < ages.size(); i++) {
std::cout << ages[i] << "\n";
}
return 0;
}
```

With the above code, the ages vector turns into `{5, 7, 8, 12, 4}` because 8 is inserted at index 2.

Now, you probably notice one odd thing with this code: the use of `ages.begin()`. Inserting elements into a vector requires you to provide an “iterator,” not an index. By adding the index you want to use (2 in the code above) to `ages.begin()` (which can be thought of as index 0), you get an “iterator” representing the right location inside of the vector.

(Why does inserting into a vector require an iterator? It definitely seems unnecessary and overly complicated. And, for just vectors, it probably is. However, there are some other C++ “containers” (things used for containing groups of data, like the vector) that work a little differently from the vector, causing them to not work well with normal indices. Therefore, to make functions like inserting elements consistent across these “containers,” iterators are used. This is something you definitely don’t need to worry about too much, though. Just know that inserting uses iterators.)

##### Removing an Element from Anywhere in a Vector

To remove any vector element, use “`.erase(iteratorGoesHere)`“, which requires an iterator (not an index), just like vector insertion. Doing so causes the length of the vector to decrease by 1, and elements following the erased element are shifted up:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
ages.erase(ages.begin() + 1);
for (int i = 0; i < ages.size(); i++) {
std::cout << ages[i] << "\n";
}
return 0;
}
```

With the above code, the ages vector turns into `{5, 12, 4}` because the element at index 1 (the number 7) is removed.

##### Removing All Elements from a Vector

To clear a vector of all its elements (reducing its length to 0), just use “`.clear()`“:

```#include <iostream>
#include <vector>

int main() {
std::vector<int> ages = {5, 7, 12, 4};
ages.clear();
std::cout << ages.size() << "\n";
return 0;
}
```

### When to Use Arrays

With all the benefits vectors have to offer, it may be tempting to discount arrays entirely. However, arrays are great for containing a list of data that doesn’t ever change in length, mainly because they tend to perform slightly better than vectors. On modern computers, this difference isn’t entirely significant, but it’s usually good to stick with arrays if you won’t ever need to `push_back`, `pop_back`, or use any of the vector’s other special functionality.

### Challenge Problem

To the program below, add code that loops through each element of the given vector and removes every even number. At the end of the program, print each element in the new vector.

```#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers = {5, 7, 3, 4, 2, 5, 8, 9, 9, 0};
return 0;
}
```

(The elements 4, 2, 8, and 0 should be removed.)

Hint: When writing this program, be very careful. Removing an element from a vector shifts all following elements to the left. Try to figure out how this can be a problem, and think of ways to solve this.

If you get stuck, check out my solution here.