Arrays and Vectors are considered as a collection of elements having the same data type where elements are stored in neighboring allocations in the memory. Each element is identified by at least one array index or key and this element or key is used for data access. These data types are typically used to store and manage a large number of objects using only one variable. Then instead of creating (variable_1, variable_2, variable_3), you can stack them into one container and easily manipulate and access them.

The simplest type of arrays or vectors is linear ones. However, 2-D containers and higher dimensional structures can be created and used according to the application. For example, in computer vision, 2-D vectors are utilized to represent greyscale images as matrices and higher-dimensional vectors are used to represent RGB images. ## Getting Started with Arrays

To create and work with arrays, no need to include any extra header files in C++. Just go ahead and initialize your variable.

### Array declaration

Typically there are 3 ways to declare your array:

* Declare array by setting a fixed size

Example: declare 3 integer arrays of fixed size 5, 10, and 15.

```int arr_1;
int arr_2;
int arr_3;
```

* Declare array by initializing your elements

Example: declare 3 integer arrays by initializing elements.

```int arr_1[] = {5, 10, 15};
int arr_2[] = {20, 25, 30};
int arr_3[] = {35, 40, 35};
```

* Declare array by setting fixed size and initializing your elements
Note that you can create an array with size 10 and only initialize the first 5 elements.

Example: declare 3 integer arrays using both, setting fixed sizes and initializing elements.

```int arr_1 = {1, 2, 3};
int arr_2 = {1, 2, 3}; /* array of size 4 with first 3 elements initialized */
int arr_3 = {1, 2, 3}; /* array of size 5 with first 3 elements initialized */
```

## Array elements are stored contiguously

To illustrate how array elements are stored in a contiguous manner, have a look at the following example.

Example program to:

• Declare array with 3 elements
• Print the addresses of the array elements
```#include <iostream>
using namespace std;

int main()
{
/* declare array with 3 elements */
int arr[] = { 1, 2, 3 };

/* print address of 1st 3 elements */
cout << &arr << endl
<< &arr << endl
<< &arr << endl;

return 0;
}
```

Output:

Notice how the difference between array elements is only 4 bytes, which is the integer size in this case.

00F3F7A0
00F3F7A4
00F3F7A8

## Accessing and changing array elements

After declaring your array, you need to make use of the saved elements by accessing them at runtime. One way to do this is by using the square bracket “[ ]” operator.

Note that the index count of arrays and vectors in C++ starts at 0 and not 1. In other words, the index of the second element is going to be 1 and not 2.

Example program to:

• Declare integer array with 4 elements
• Print second element
• Access the second element
• Add value 5 to the second element
• Print second element
```#include <iostream>
using namespace std;

int main()
{
/* declare array with 4 elements */
int arr[] = { 5, 10, 15, 20 };
cout << "Second element at index 1: " << arr << endl;

/* edit 2nd value */
arr += 5;
cout << "Second element at index 1: " << arr << endl;

return 0;
}
```

Output:

Second element at index 1: 10
Second element at index 1: 15

## Looping through array elements

In most cases, to manipulate the whole array and not just single elements, you need to loop through it.

Example program to:

• Declare array with size 10
• Fill the array with integer numbers from 1 to 10
• Print array
• Add the value of 10 to all array elements
• Print array
```#include <iostream>
using namespace std;

int main()
{
/* declare array with size 10 */
int arr;

/* loop to fill array */
for (int i = 0; i < 10; i++)
{
arr[i] = i + 1;
}

/* print array */
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cout << endl;

/* add value of 10 to all elements */
for (int i = 0; i < 10; i++)
{
arr[i] += 10;
}

/* print array */
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
```

Output:

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20

## Working with Vectors

Vectors are used and favored over arrays mainly because arrays are statically allocated in the memory while vectors can be dynamically allocated at runtime. In addition to a bunch of useful functions for iteration and size manipulation that comes with the Vector class.

### Getting started
To use vectors, an extra header file needs to be included.

#include <vector>

## Vector Declaration

Mostly, vectors are created to have dynamic size as this is one of its biggest advantages over arrays. However, you can still declare a vector with a fixed size in case you need to manage your memory in some way.

Example vector of integers declaration:

vector<int> vector_1;

Example vector of integers declaration with a fixed size of 10.

vector<int> vector_2(10);

## Functions for iteration

• begin( ): function to return an iterator pointing to the first element in the vector
• end( ): function to return an iterator pointing to the element after the last element in the vector, which could possibly be an empty space in the memory.
• rbegin( ): function to get a reverse iterator pointing to the last element in the vector but in this case moves from the last to the first element.
• rend( ): function to get a reverse iterator pointing to the element before the first element in the vector in case you need to iterate reversely.

Example program to:

• Declare empty vector with a fixed size of 10
• Loop through the vector using iterator functions and fill with integer values
• Loop through the vector using iterator functions and print the elements
• Loop through the vector using iterator functions and print the elements in reversed order.
```#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector <int> myVect(10);

/* create iterators pointing at begin and end */
vector <int>::iterator it_begin =  myVect.begin();
vector <int>::iterator it_end = myVect.end();

/* create reverse iterators pointing at begin and end */
vector <int>::reverse_iterator it_rbegin = myVect.rbegin();
vector <int>::reverse_iterator it_rend = myVect.rend();

/* fill vector with nums from 1 to 10 */
for (int i = 0; i < 10; i++)
{
myVect[i] = i + 1;
}

/* print vector */
for (vector<int>::iterator it = it_begin; it < it_end; it++)
{
cout << *it << " ";
}
cout << endl;

/* print vector in reverse order */
for (vector<int>::reverse_iterator it = it_rbegin; it < it_rend; it++)
{
cout << *it << " ";
}
cout << endl;

return 0;
}
```

Program output:

1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

## Accessing vector elements

To access a specific element in your vector one of the following methods may be used:

• using the square bracket [ ] operator

`int element = myVect[index];`

• using the at( ) function

`int element = myVect.at(index);`

Both methods yield the same result and have the same functionality, you can choose to use whichever is convenient.

Typical operations that you would need to do with your vector are add new elements, remove elements, or even erase all elements.

• push_back( ): allows you to add a number to the end of the vector.
• pop_back( ): helps you remove the last element in your vector.
• insert( ): this function allows you to insert an element at a given index or position.
• erase( ): enables you to remove elements from vector from specified indices or range.
• clear( ): this one removes all elements from your vector container.

Example, write a program that does the following:

• Declare empty vector of integers with no fixed size
• Add integer numbers to vector ranging from 1 to 20
• Print vector
• Remove integers with index range above 4
• Print vector
• Insert integer numbers ranging from 5 to 10
• Print vector
• Delete all elements in the vector
```#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector <int> myVect;

/* fill vector with nums from 1 to 20 */
for (int i = 0; i < 20; i++)
{
myVect.push_back(i + 1);
}

/* print vector */
for (vector<int>::iterator it = myVect.begin(); it < myVect.end(); it++)
{
cout << *it << " ";
}
cout << endl;

/* remove nums above 5 */
myVect.erase(myVect.begin() + 5, myVect.end());

/* print vector */
for (vector<int>::iterator it = myVect.begin(); it < myVect.end(); it++)
{
cout << *it << " ";
}
cout << endl;

/* insert numbers from 6 to 10 */
for (int i = 5; i < 10; i++)
{
myVect.insert(myVect.begin() + i, i + 1);
}

/* print vector */
for (vector<int>::iterator it = myVect.begin(); it < myVect.end(); it++)
{
cout << *it << " ";
}
cout << endl;

/* delete all elements */
myVect.clear();

return 0;
}
```

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10

## Functions for size manipulation

Properly handling the size of your vector is a good practice in case you need to take good care of the memory in your program. You might need to avoid any extra allocated memory or even know the size of your vector.

• size( ): simply tells you the size of your vector or the number of elements.
• capacity( ): tells you the memory allocated to this vector
• shrink_to_fit: reduces the memory allocated to your vector to only match the number of elements.
• resize( ): reshapes your container to contain only a specific amount of elements.
• empty( ): returns a boolean telling you whether the vector is empty or not.

Example program to:

• Initialize empty vector of integers
• Print the size and capacity of the vector
• Add 5 elements to the vector
• Print the size and capacity of the vector
• Shrink the size of the vector to match the number of elements
• Print the size and capacity of the vector
• Resize your vector to only have 3 elements instead of 5
• Print the size and capacity of the vector
• Clear all elements in the vector
• Print whether the vector is empty or not
```#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector <int> myVect;

/* print size and capacity */
cout << "vector size: " << myVect.size() << endl;
cout << "vector capacity: " << myVect.capacity() << endl;

cout << endl << "Added 5 elements" << endl;
for (int i = 0; i < 5; i++)
{
myVect.push_back(i + 1);
}

/* print size and capacity */
cout << "vector size: " << myVect.size() << endl;
cout << "vector capacity: " << myVect.capacity() << endl;

/* shrink to fit */
cout << endl << "Shrink to fit the vector" << endl;
myVect.shrink_to_fit();

/* print size and capacity */
cout << "vector size: " << myVect.size() << endl;
cout << "vector capacity: " << myVect.capacity() << endl;

/* resize to 3 */
cout << endl << "Resize the vector to 3 elements" << endl;
myVect.resize(3);

/* print size and capacity */
cout << "vector size: " << myVect.size() << endl;
cout << "vector capacity: " << myVect.capacity() << endl;

/* clear all elements */
cout << endl << "Clearing all elements" << endl;
myVect.clear();

/* is empty or not? */
cout << "Is my vector empty? ";

if (myVect.empty())
cout << "TRUE" << endl;
else
cout << "FALSE" << endl;

return 0;
}
```

Output:

vector size: 0
vector capacity: 0