The C++ Standard Template Library (STL) is a powerful collection of template classes and functions that provide general-purpose classes and algorithms. One of the fundamental components of the STL is the vector
class, which offers a dynamic array implementation. The vector
class simplifies array handling by providing dynamic resizing, automatic memory management, and a wide range of member functions. In this article, we will delve into the features and usage of the vector
class in C++.
Basics of the Vector Class
The vector
class is part of the <vector>
header in the C++ Standard Template Library. It is a dynamic array that can grow or shrink in size, allowing for flexible storage of elements. Here is a basic declaration of a vector:
#include <vector>
int main() {
// Declare an integer vector
std::vector<int> myVector;
// Add elements to the vector
myVector.push_back(10);
myVector.push_back(20);
myVector.push_back(30);
// Access elements
int element = myVector[1]; // Retrieves the element at index 1 (20)
return 0;
}
In this example, we create an integer vector (std::vector<int>
) and add elements to it using the push_back
member function. The push_back
function is used to insert elements at the end of the vector.
Dynamic Resizing
One of the key advantages of the vector
class is its ability to dynamically resize itself. As elements are added, the vector can automatically allocate more memory to accommodate the growing size. This ensures efficient memory usage without the need for manual memory management.
cpp
#include <vector>
int main() {
std::vector<int> dynamicVector;
for (int i = 0; i < 10; ++i) {
dynamicVector.push_back(i);
}
// The vector dynamically adjusts its size to accommodate the added elements
return 0;
}
In this example, the vector starts empty, but as elements are added in the loop, the vector’s size dynamically increases.
Iterating Over a Vector
We can use iterators to traverse the elements of a vector. Here’s an example:
#include <iostream>
#include <vector>
int main() {
std::vector<int> myVector = {1, 2, 3, 4, 5};
// Iterate over the vector using iterators
for (auto it = myVector.begin(); it != myVector.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
This code prints each element of the vector to the console. The begin()
and end()
member functions return iterators pointing to the first and one-past-the-last elements of the vector, respectively.
Common Member Functions
The vector
class provides a variety of member functions for performing operations on the vector. Some common ones include:
size()
: Returns the number of elements in the vector.empty()
: Returnstrue
if the vector is empty,false
otherwise.clear()
: Removes all elements from the vector.pop_back()
: Removes the last element from the vector.front()
andback()
: Return references to the first and last elements, respectively.
Conclusion
The vector
class in C++ is a versatile and powerful container that simplifies dynamic array handling. Its dynamic resizing, automatic memory management, and extensive set of member functions make it a valuable tool for developers. Whether you are working with small collections or managing large datasets, the vector
class provides a flexible and efficient solution for your array needs in C++.
That’s all for now. Stay tuned for more in the near future.
Further Readings
If you liked this article, then please subscribe to our YouTube Channel. You can also find us on Instagram, Facebook and Twitter.
READ – CONNECT – BOOST – CREATE