Key features of Vectors

Key Features:

  1. Dynamic Sizing: Vectors can grow or shrink at runtime, making them more flexible than arrays whose size is fixed at compile-time.

  2. Contiguous Storage: Like arrays, vectors store their elements in contiguous memory locations, which is beneficial for cache locality and thus performance.

  3. Automatic Management: Vectors handle memory allocation and deallocation automatically. When the vector grows beyond its current capacity, it automatically allocates a larger block of memory, copies the existing elements, and frees the old memory.

  4. Random Access: Vectors support constant-time access to individual elements, both for reading and writing.

  5. Type-Safety: Vectors are type-safe containers, meaning that they can only store elements of the same type.

  6. STL Compatibility: Being part of the STL, vectors are compatible with many built-in algorithms and functions provided by the library.

Common Operations:

  1. Insertion: Elements can be added to the end of a vector using the push_back method. This operation is generally fast, taking constant time on average.

  2. Deletion: Elements can be removed from the end using the pop_back method. This operation is constant time.

  3. Access: Elements can be accessed using the [] operator or the at method. The [] operator does not perform bounds checking, while at does.

  4. Iteration: You can iterate through a vector using iterators, range-based for loops, or traditional for loops.

  5. Searching and Sorting: While vectors themselves do not provide built-in search or sort functionalities, you can easily use STL algorithms like std::find and std::sort on them.

  6. Size and Capacity: You can get the number of elements in a vector using the size method and the current capacity using the capacity method. You can also manually change the capacity using methods like resize and reserve.

Range-based for loops

In C++, a range-based for loop is a convenient way to iterate over container elements like a vector. It provides a simpler and more readable syntax compared to traditional for loops.

Here's a basic example to illustrate how you can use a range-based for loop with a vector:

#include <iostream> #include <vector> using namespace std; int main() { vector<int> myVector = {1, 2, 3, 4, 5}; // Range-based for loop for (int value : myVector) { cout << value << endl; } return 0; }

In this example, myVector is a vector of integers. The range-based for loop iterates over each element in myVector. For each iteration, the variable value is assigned the current element from the vector, and then it's printed out.

Key Points:

  1. Syntax: The range-based for loop has the syntax for (declaration : range) { /*...*/ }. In this case, int value is the declaration, and myVector is the range.

  2. Readability: This loop style is more readable and less error-prone, especially when dealing with complex data structures.

  3. Modifying Elements: If you need to modify the elements of the vector, you should use a reference in the loop:

    for (int &value : myVector) { value *= 2; // This will modify the elements in the vector }

This feature of C++ makes code involving container iteration much more concise and less prone to errors like off-by-one mistakes or incorrect container bounds.

 

Here's a simple example to declare a vector, add elements to it, and access them:

#include <vector> #include <iostream> using namespace std; int main() { vector<int> myVector; // Adding elements myVector.push_back(1); myVector.push_back(2); myVector.push_back(3); // Accessing elements cout << "First element: " << myVector[0] << endl; // Iterating through elements for(int num : myVector) { cout << num << " "; } cout << endl; return 0; }

Vectors are one of the most commonly used data structures in C++ due to their flexibility and ease of use.

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