Arrays & Vectors
Arrays are a collection of variables of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier.
Arrays have several characteristics:
- Size of the array is fixed
- Variables are of the same type
- Stored in contiguous memory locations
- Each element can be accessed by using an index (position)
- Position of first element is
0not1
To declare an array, we use the following syntax:
For example:
double values[10]; // array of 10 doubles
To intialize an array there are multiple ways:
- Initialize each element individually
- Intialize first few elements and rest will be set to
0
- Intialize all elements to
0
int numbers[5] = { 0 }; // array of 5 integers -> { 0, 0, 0, 0, 0 }
- Size automatically calculated by compiler
To access an element in an array, we use the following syntax:
For example:
cout << numbers[0] << endl; // 1
cout << numbers[1] << endl; // 2
numbers[3] = 10; // assign 10 to 4th element
The name of the array represents the address of the first element in the array in memory. This is why we can use the array name as a pointer to the first element in the array. The index of the array represents the offset from the first element in the array.
Warning
You have to do the bounds checking yourself. If you try to access an element outside the bounds of the array, you will get undefined behavior.
Multidimensional arrays are arrays of arrays, where each element is itself an array. You have to be aware that some compilers limit the number of dimensions you can have in an array.
To declare a multidimensional array, we use the following syntax:
For example:
To initialize a multidimensional array, we use the following syntax:
{ 1, 2, 3 },
{ 4, 5, 6 }
}; // array of 2 arrays of 3 integers
To access an element in a multidimensional array, we use the following syntax:
Vectors are similar to arrays, where they are collections of variables of the same type, it is provided by the C++ Standard Library. Vectors are more flexible than arrays which provides the following characteristics:
- Size of the vector is dynamic
- Variables are of the same type
- Stored in contiguous memory locations
- Each element can be accessed by using an index (position)
- Position of first element is
0not1 - Provides methods to add and remove, sort, and ...
To use vectors, we have to include the header file, and to declare a vector, we use the following syntax:
#include <vector>
using namespace std;
int main() {
vector<int> numbers; // Empty vector of integers
vector<double> values (10); // Vector of 10 doubles initialized to 0.0
}
Unlike arrays, when we declare a vector the values of the vector are intialized to 0 incase of integers and 0.0 incase of doubles, ...
We can also initialize a vector with values, we use the following syntax:
You can intialize a vector with a specific size, and specify a value for all elements, we use the following syntax:
To access an element in a vector, we can do it in two ways:
- Using the
[]operator
cout << numbers[0] << endl; // 1
- Using the
at()method
The at() method is safer than the [] operator, because it does bounds checking, and if you try to access an element outside the bounds of the vector, it will throw an exception during runtime instead of undefined behavior.
To add an element to a vector, we use the method push_back(), as follows:
numbers.push_back(6); // { 1, 2, 3, 4, 5, 6 }
To remove an element from a vector, we use the method pop_back(), as follows:
numbers.pop_back(); // { 1, 2, 3, 4 }
To get the size of a vector, we use the method size(), as follows:
cout << numbers.size() << endl; // 5
To create a multidimensional vector, we use the following syntax:
{ 1, 2, 3 },
{ 4, 5, 6 }
};
To access an element in a multidimensional vector, we use the following syntax:
cout << numbers.at(0).at(1) << endl; // 2