Arrays and Vector preview examples in C++

An array is a collection of variables of the same data type. These variables are stored in a contiguous block of memory. In C++, you can declare an array by specifying the data type, the name of the array, and the size of the array. For example, the following code declares an array of integers called “numbers” with a size of 5:

int numbers[5];

This array has 5 elements, numbered 0 through 4. You can access each element of the array using its index, which is the number inside the square brackets. For example, to assign the value 10 to the first element of the array, you would use the following code:

numbers[0] = 10;

You can also initialize the array with values at the time of declaration, for example:

int numbers[5] = {1, 2, 3, 4, 5};

You can also use a loop to iterate through the elements of an array and perform some operation with them. For example, the following code prints out all the elements of the array “numbers”:

for (int i = 0; i < 5; i++) {
    cout << numbers[i] << endl;
}

It’s important to note that arrays have a fixed size and you cannot change it once it’s declared, if you need to increase or decrease the size of an array, you will have to create a new array and copy the elements from the old array to the new one.

In C++, a vector is a container class that is part of the Standard Template Library (STL). Vectors are similar to arrays, but with the added advantage of being able to dynamically grow or shrink in size. Here are a few examples of how vectors can be used in C++:

Creating a vector: To create a vector, you can use the vector class template and specify the type of elements it will contain. For example, to create a vector of integers, you can use the following code:

std::vector<int> myVector;

Adding elements to a vector: To add elements to a vector, you can use the push_back() function. For example, the following code adds the integers 1, 2, and 3 to the vector:

myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
  1. Accessing elements in a vector: To access elements in a vector, you can use the index operator ([]). For example, the following code prints the second element of the vector:
std::cout << myVector[1] << std::endl; // prints 2

4. Removing elements from a vector: To remove elements from a vector, you can use the pop_back() function to remove the last element, or the erase() function to remove a specific element. For example, the following code removes the last element of the vector:

myVector.pop_back();

5. Iterating over a vector: To iterate over the elements of a vector, you can use an iterator, which is a special type of pointer that can be used to traverse the elements of a container. For example, the following code iterates over the elements of the vector and prints each one:

for (std::vector<int>::iterator it = myVector.begin(); it != myVector.end(); ++it)
{
    std::cout << *it << std::endl;
}

In summary, vectors and arrays are similar, but vectors have the added advantage of being able to change size, and they come with additional functionality. When you are programming, you can choose to use either arrays or vectors depending on your needs and preferences.

To know the size of a vector: You can use the size() function to find out how many things are in the vector. For example, the following code prints the number of elements in the vector:

std::cout << myVector.size() << std::endl;

Resizing a vector: You can use the resize() function to change the number of elements that a vector can hold. This can be used to make a vector larger or smaller. For example, the following code increases the size of the vector to 5 elements:

myVector.resize(5);

Sorting a vector: You can use the sort() function to sort the elements of a vector. For example, the following code sorts the elements of the vector in ascending order:

std::sort(myVector.begin(), myVector.end());

Swapping two vectors: You can use the swap() function to swap the contents of two vectors. For example, the following code swaps the contents of the vectors myVector1 and myVector2:

std::vector<int> myVector1, myVector2;
// add elements to myVector1 and myVector2
myVector1.swap(myVector2);

Clearing a vector: You can use the clear() function to remove all the elements from a vector. This function does not change the size of the vector, it just removes all the elements. For example, the following code removes all the elements from the vector:

myVector.clear();

Here is a link that you can learn more about the different types of functions about Vectors:

https://cplusplus.com/reference/vector/vector/

https://www.programiz.com/cpp-programming/arrays

https://www.w3schools.com/cpp/cpp_arrays.asp

https://www.geeksforgeeks.org/vector-in-cpp-stl/

https://www.mygreatlearning.com/blog/vectors-in-c/

Here are some practice exercises to help you understand:

  1. Create an array of integers and a vector of integers with the same elements. Then use a for loop to print out each element of the array, and use an iterator to print out each element of the vector.
  2. Create a vector of strings, and use a for loop to add several words to the vector. Then use an iterator to print out each element of the vector in reverse order.
  3. Create an array of doubles and a vector of doubles with the same elements. Then use the sort() function to sort the elements of the array and the vector in descending order, and use a for loop to print out each element of the array, and use an iterator to print out each element of the vector.
  4. Create a vector of integers, and use the push_back() function to add several numbers to the vector. Then use the pop_back() function to remove the last element of the vector, and use an iterator to print out each element of the vector.
  5. Create an array of characters and a vector of characters with the same elements, then using a for loop concatenate all the elements of the array and the vector in a string variable and print it.
  6. Create a 2D array of integers and a vector of vectors of integers with the same elements and use nested loops to print out each element.

This is all for now!

Peace!