C++ Vectors
Tags: cpp
Published: 2021-01-09 02:51:48

---
title: C++ Vectors 
published: true
description: C++ Vectors
tags: cpp
series: C++ Journey
//cover_image: https://direct_url_to_image.jpg
---

A vector is similar to a standard array, but can be dynamic. This means it does not have a fixed size and can be resized as needed. It is part of the Standard Template Library (STL) and must be included in your file with `#include <vector>`.

Like regular arrays, you specify a data type and name. You can specify a size to initialize the vector if you want, and you can also specify what you want to initialize each index of the vector with. You can also initialize a vector with the contents of another vector.

```
std::vector<int> numsA;
std::vector<int> numsB(5);
std::vector<int> numsC(5, 9);
std::vector<int> numsD(numsC);
```

`numsA` is a vector of size 0. `numsB` is a vector of size 5 and each index is initialized with the value 0. `numsC` is a vector of size 5 and each index is initialized with the value 9. Finally, `numsD` is initialized to copies of the contents of `numsC`, which makes it a vector of size 5 filled with 9s as well.

## Manipulation
You can retrieve the element stored at an index as well as alter the value at that index.

* `v.at(index)` returns the element at the specified index
* `v[index]` returns the element at the specified index
* `v.front()` returns the first element
* `v.back()` returns the last element

Keep in mind, `front()` and `back()` do not check to make sure the vector is empty or not. If it is, your code will compile but it will halt and you'll get a `EXC_BAD_ACCESS` error. You will also get an `out_of_range` error if you try to use `at()` with an index that is out of range of the one you created.

```
// create a char array of size 4
std::vector<char> alpha(4);

// fill values at specific indices
alpha.front() = 'A';
alpha[1] = 'B';
alpha.at(2) = 'C';
alpha.back() = 'D';

// display values at specific indices
std::cout << alpha.front() << "\n";
std::cout << alpha[1] << "\n";
std::cout << alpha.at(2) << "\n";
std::cout << alpha.back() << "\n";

// output
A
B
C
D
```

## Member Functions
There are many functions on the class vector you can use:

* `v.capacity()` returns the max number of elements that can be stored in the vector without having to reallocate more space
* `v.empty()` returns true (1) if the vector is empty; false (0) if not
* `v.size()` returns the size of the vector
* `v.max_size()` returns the max number of elements that can be inserted into the vector

```
std::vector<char> alpha(4);

std::cout << alpha.empty() << "\n";
std::cout << alpha.capacity() << "\n";
std::cout << alpha.size() << "\n";
std::cout << alpha.max_size() << "\n";

// output
0
4
4
9223372036854775807
```

* `v.clear()` deletes all the elements from the vector

```
// initialize 4 elements to 'A'
std::vector<char> alpha(4, 'A');

std::cout << alpha.empty() << "\n";
alpha.clear();
std::cout << alpha.empty() << "\n";

// output
0
1
```

* `v.push_back()` pushes an element into the end of the vector
* `v.pop_back()` removes the element at the end of the vector

```
// create empty vector
std::vector<char> alpha;

// push elements into vector
alpha.push_back('A');
alpha.push_back('B');
alpha.push_back('C');

alpha.pop_back(); // remove last element 'C'

// loop through elements of vector and display values
for (int i = 0; i < alpha.size(); i++)
  std::cout << i << ": " << alpha[i] << std::endl;

// output
0: A
1: B
```

You can find more member functions [here](https://www.cplusplus.com/reference/vector/vector/).

## Disadvantage
Since vectors are dynamic they may take up more space than they need for the elements they contain, so that more elements can be added. When they need to be resized to fit more elements, memory has to be reallocated. Therefore, vectors consume more memory and there is some performance overhead compared to arrays.

### Further Reading / References
* C++ Programming: Program Design Including Data Structures 8th ed. by D.S. Malik
* [Cplusplus.com - std::vector](https://www.cplusplus.com/reference/vector/vector/)