common vehicle vs vector

Vehicle vs Vector: Common Misconceptions

By Daniel Smith
AD

In the world of data structures, arrays and linked lists are two of the most fundamental concepts. While both are used to store and manage a collection of data, they differ in their implementation and usage. Today, we're delving into a comparison of the common vehicle, the array, and its counterpart, the vector, in the C++ Standard Template Library (STL).

The array is a linear static data structure that can hold a fixed number of elements. It's simple and fast, offering constant time complexity O(1) for access and search operations. On the other hand, the vector is a dynamic array, also found in the STL, which can grow and shrink in size as needed. It offers more flexibility but comes with a trade-off in speed.

Initialization and Resizing

Arrays, once initialized, have a fixed size. This means that you can't add or remove elements from an array without creating a new one. Vectors, however, are dynamic and can grow or shrink as needed. They do this by continually reallocating memory under the hood, a process known as reallocation.

AD
a poster with different types of cars and trucks on it's sides, including the words match the similar
a poster with different types of cars and trucks on it's sides, including the words match the similar
four different types of vehicles are shown in this image, including a bus, a car, and a motorcycle
four different types of vehicles are shown in this image, including a bus, a car, and a motorcycle
Pictographs | Pictorial Representation | Pictorial Symbols | Symbols
Pictographs | Pictorial Representation | Pictorial Symbols | Symbols

In C++, arrays are typically declared like so: `type arrayName[arraySize];`, while vectors are usually declared and initialized using the constructor, which can take an initial size and elements: `std::vector vecName(initialSize, initialElements);`.

Array Initialization

Array initialization is straightforward but offers limited flexibility. For instance, you can't change the size of the array after it's been initialized. If you don't specify the size during declaration, the compiler will set it to the number of elements specified in the initializer list:

the different types of vehicles are shown in this diagram
the different types of vehicles are shown in this diagram
Bus versus Car Comparing Vector Illustration Illustration #83076128
Bus versus Car Comparing Vector Illustration Illustration #83076128
three different types of vehicles and a plane
three different types of vehicles and a plane

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

Vector Initialization

Vectors, on the other hand, offer more options. You can specify the initial size, or let the vector figure it out based on the number of elements you provide:

AD

`std::vector vec1(5, 0);` // vec1 has 5 elements, all initialized to 0
`std::vector vec2{1, 2, 3}`;

various types of vehicles and people on motorbikes
various types of vehicles and people on motorbikes
three different types of cars and motorcycles are depicted in this image, one is red
three different types of cars and motorcycles are depicted in this image, one is red
diferentes vehículos, conjunto. coche, moto, bicicleta, scooter, ciclomotor, camión. iconos de transporte. ilustración vectorial en estilo plano.
diferentes vehículos, conjunto. coche, moto, bicicleta, scooter, ciclomotor, camión. iconos de transporte. ilustración vectorial en estilo plano.
Vehicle vs Parking Vocabulary
Vehicle vs Parking Vocabulary
Xe
Xe
four different red cars on a white background
four different red cars on a white background

Accessing Elements

Both arrays and vectors allow direct access to elements using the index notation, which can be quite useful in various scenarios. However, access in arrays is faster due to the contiguous memory allocation.

In C++, you access an array's nth element with `arrayName[n]` and a vector's nth element with `vecName[n]`.

AD

Array Access

The accesses are direct and simple. Arrays are stored contiguously in memory, which makes access to elements fairly quick. But remember, once an array is full, you can't add more elements without creating a new array.

four different types of vehicles on a wooden surface, including a bus, a truck and a tractor
four different types of vehicles on a wooden surface, including a bus, a truck and a tractor

Vector Access

Vectors use grows-and-shrinks strategy, adding elements at the end of the vector. Accessing elements is also straightforward with the same index notation as arrays, but it's slightly slower as vectors might not be contiguous in memory due to reallocations.

Inserting and Removing Elements

Arrays do not support insertion or removal of elements without copying all the elements to a new array. Vectors, however, offer several member functions to insert and remove elements.

AD

The `insert` function for vectors can add elements at any location, breaking the contiguity of existing elements. The `push_back` function adds elements at the end of the vector. Removal can be accomplished with `pop_back`, `erase`, or `remove`.

Array Insertion and Removal

Insertion or removal in arrays would require a new array of the new size, with all elements copied over, leading to a significant overhead. The same applies to removal of an element.

The array would look like this:
`int arr[4] = {1, 2, 3, 4};`
After inserting an element at index 1, it becomes:
`int arr[5] = {1, 0, 2, 3, 4};`

Vector Insertion and Removal

Vectors, due to their dynamic nature, allow addition and removal of elements. `insert`, `push_back`, `pop_back`, `erase`, or `remove` functions can be used for insertion and removal, adjusting the size of the vector as needed:

`std::vector vec{1, 2, 3};`
After inserting an element at the beginning, it becomes:
`std::vector vec{4, 1, 2, 3};`

In summary, arrays and vectors in C++ STL cater to different use cases, depending on whether you need constant-time access, fixed-size elements, or dynamic downsizing. The choice between the two would largely depend on the specific requirements of your project.

AD