Vector are similar to arrays but can dynamically change size.
Include the vector library and create a vector with:
std::vector<type> <name>;
#include <vector>
std::vector<char> alphabet;
// or
std::vector<char> alphabet = {'a', 'b', 'c'};
Note: the type cannot be changed after its declaration.
Add element at the end of a vector with .push_back()
std::vector<int> my_numbers;
my_numbers.push_back(2);
my_numbers.push_back(6);
// my_numbers contains {2, 6}
Remove the last element with .pop_back().
my_numbers.pop_back();
Access a given index with brackets []
std::cout << my_numbers[2];
Access the first and last and first element with .front() and .back()
std::cout << my_numbers.front();
std::cout << my_numbers.back();
Get the number of elements with .size()
std::vector<std::string> colors = {"Red", "Green", "Blue"};
std::cout << colors.size(); // outputs: 3
.empty() return 0 if a vector is empty and 1 otherwise.
Use for loop (with vector.size()) or a for-each loop.