Vectors

What is a Vector

In C++, vectors are a dynamic data structure that allows you to store a collection of elements of the same data type. Unlike arrays, vectors can be resized dynamically during runtime, which makes them more flexible.

A vector in programming can be thought of as a more flexible version of an array. Here's a simple explanation:

  1. Dynamic Array: Think of a vector as a dynamic array. Like an array, it's a collection of elements that are stored in a sequence. But unlike a standard array, a vector can change its size automatically when you add or remove elements.

  2. Growing and Shrinking: Imagine a train that can add or remove carriages as needed. A vector works similarly. If you need more space to add elements, it automatically expands. If you remove elements and don’t need as much space, it can shrink.

  3. Easy to Use: Vectors handle all the resizing work for you. This means you don’t have to worry about the size of the vector as you would with a traditional array. You can just keep adding elements to it.

  4. Accessing Elements: Just like an array, you can access elements in a vector by their position, using an index. For instance, you can ask for the first, fifth, or tenth element.

  5. Versatile and Safe: Vectors are versatile and safer to use than standard arrays. They provide functionalities like checking the size, adding elements at any position, removing elements, and more, without worrying about going out of bounds (unless you access them incorrectly).

  6. Behind the Scenes: Internally, when a vector grows, it may need to allocate a larger block of memory to accommodate the new elements. If there’s not enough space in its current location, it might move to a new location in memory. This is all handled automatically, making vectors a very convenient data structure to use.

In summary, vectors in programming are like smarter, more flexible arrays. They can grow and shrink automatically and provide many functionalities that make them easier and safer to use than traditional arrays. You will take a deeper look into vectors in Programming 3

To use vectors in C++, you must include the <vector> header file. To declare a vector, you can use the following syntax:

#include <vector> using namespace std; vector<int> myVector;

This creates an empty vector called myVector that can store integer values. You can also initialize a vector with some initial values using the following syntax:

vector<int> myVector = {1, 2, 3, 4, 5};

This creates a vector called myVector with 5 integer elements.

You can add elements to a vector using the push_back() method. For example:

myVector.push_back(6);

This adds the value 6 to the end of myVector.

You can access an element in a vector using the index of the element within square brackets after the vector name. For example:

This sets the third element of myVector to 10.

You can use loops to iterate over the elements of a vector. For example:

This will print out all the elements of myVector separated by a space.

Vectors in C++ are a powerful data structure that can be used in a wide range of applications. They provide many advantages over arrays, such as dynamic resizing and easier memory management.

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark