Key features of Vectors
Key Features:
Dynamic Sizing: Vectors can grow or shrink at runtime, making them more flexible than arrays whose size is fixed at compile-time.
Contiguous Storage: Like arrays, vectors store their elements in contiguous memory locations, which is beneficial for cache locality and thus performance.
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.
Random Access: Vectors support constant-time access to individual elements, both for reading and writing.
Type-Safety: Vectors are type-safe containers, meaning that they can only store elements of the same type.
STL Compatibility: Being part of the STL, vectors are compatible with many built-in algorithms and functions provided by the library.
Common Operations:
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.Deletion: Elements can be removed from the end using the
pop_back
method. This operation is constant time.Access: Elements can be accessed using the
[]
operator or theat
method. The[]
operator does not perform bounds checking, whileat
does.Iteration: You can iterate through a vector using iterators, range-based
for
loops, or traditionalfor
loops.Searching and Sorting: While vectors themselves do not provide built-in search or sort functionalities, you can easily use STL algorithms like
std::find
andstd::sort
on them.Size and Capacity: You can get the number of elements in a vector using the
size
method and the current capacity using thecapacity
method. You can also manually change the capacity using methods likeresize
andreserve
.
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:
Syntax: The range-based for loop has the syntax
for (declaration : range) { /*...*/ }
. In this case,int value
is the declaration, andmyVector
is the range.Readability: This loop style is more readable and less error-prone, especially when dealing with complex data structures.
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