0.1 Arrays and Vectors
One of the most important and essential topic in computer science in general could be arrays and vectors and they provide a way to effectively store and manipulate data. Let’s get started with arrays!
0.1.1 Arrays
A simple array of \(n\) elements can be thought of as an \(n\)-tuple.
Definition 0.1.1
An array is a collection of elements of the same data type.
When we declare an array, we need to specify the type, name, and the number of elements [CS02-4].
Consider the following example.
In the example above, we have an array
arr with
five elements. When we actually print them to the console with a for
loop, we can see that we have to start from i = 0 since
the index of the first element is \(0\). To access an element, we can have the
name of the array, bracket, and the index number.
We can also edit an element by specifying it.
The line arr[2] = -3 will override the previous
assignment.
Continuing, an array need not be \(1\)-dimensional. There exists multidimensional arrays that are made of lower dimensional arrays [CS02-6]. Below is an example of a \(2 \times 3\) matrix.
Code 0.1.6: CS0201.26053-03
1#include <iostream> 2 3int main() { 4 int arr[2][3]; 5 6 for (int i = 0; i < 2; i++) { 7 for (int j = 0; j < 3; j++) { 8 arr[i][j] = (i + 1)*10 + (j + 1); 9 } 10 } 11 12 for (int i = 0; i < 2; i++) { 13 for (int j = 0; j < 3; j++) { 14 std::cout << arr[i][j] << " "; 15 } 16 std::cout << "\n"; 17 } 18 19 return 0; 20}
In the example above, we declared a multidimensional array with two rows and three columns. Then, we used loops to assign a value in each index. Using the same way to put an input, we printed the values to the console. The index for the matrix should be familiar.
From the examples above, you may have noticed that array is not really flexible in terms of number of elements. Sometimes we might want to add or remove elements from an array, but it won’t allow that since the size is fixed. This is were vectors come to place.
0.1.2 Vectors
A simple way of thinking vectors is resizable array.
Definition 0.1.8
A vector is a dynamic container that contains elements of same the data type.
Vectors are provided by the Standard Template Library and we need to include directive to access them [CS02-5]. Because vectors are dynamic, we only need the data type and name of the vector when declaring.
Below is a quick example of declaring a vector and two ways of printing the elements in the console.
From the example above, we declared a vector
vec and
printed the elements to the console in two different ways. Both
methods are fine and the : symbol can be interpreted as “in.”
With the idea in mind, let’s discuss few key functions that distinguishes a vector and an array.
The first function is .push_back() function.
This function allows us to add an element as the last element. If you
wish to delete the last element, we can use the .pop_back() function.
Lastly, .size() will allow us to see the size of a
vector. Consider the following example.
Code 0.1.12: CS0201.26053-05
1#include <iostream> 2#include <vector> 3 4int main() { 5 std::vector<int> vec = {1, 2, 3, 4, 5}; 6 7 vec.push_back(6); 8 for (int x : vec) { 9 std::cout << x << " "; 10 } 11 std::cout << "\n"; 12 std::cout << vec.size() << "\n"; 13 14 vec.pop_back(); 15 for (int x : vec) { 16 std::cout << x << " "; 17 } 18 std::cout << "\n"; 19 std::cout << vec.size() << "\n"; 20 21 return 0; 22}
In this example, we declare a vector, add \(6\) to the end, list the elements, and find the size. Then, we will revert it to the original vector by removing the last element that was added.
This is it for the C++ basics! There are so much more topics to discuss in C++, but for now, let’s solve some practice problems before discussing more fundamental concepts in DSA.