Arrays of Vectors in C++ STL
Palavras-chave:
Publicado em: 07/08/2025Arrays of Vectors in C++ STL
Arrays of vectors in C++ provide a powerful and flexible way to represent and manipulate collections of data. This article explores the concept of creating and using arrays of vectors, leveraging the Standard Template Library (STL) in C++.
Fundamental Concepts / Prerequisites
To fully grasp the concepts presented in this article, you should have a basic understanding of the following:
- C++ Syntax: Familiarity with C++ data types, variables, and control flow.
- STL Vectors: Understanding how to create, manipulate, and access elements in an STL vector.
- Arrays: Basic knowledge of array declaration and usage in C++.
Core Implementation/Solution
The following code demonstrates how to declare and use an array of vectors in C++.
#include <iostream>
#include <vector>
int main() {
// Declare an array of vectors. This array has 5 elements.
// Each element is a vector of integers.
std::vector<int> arrayOfVectors[5];
// Add elements to the individual vectors within the array
arrayOfVectors[0].push_back(10);
arrayOfVectors[0].push_back(20);
arrayOfVectors[1].push_back(30);
arrayOfVectors[1].push_back(40);
arrayOfVectors[1].push_back(50);
arrayOfVectors[2].push_back(60);
// Iterate through the array of vectors and print the elements
for (int i = 0; i < 5; ++i) {
std::cout << "Vector at index " << i << ": ";
for (int j = 0; j < arrayOfVectors[i].size(); ++j) {
std::cout << arrayOfVectors[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Code Explanation
First, we include the necessary headers: <iostream>
for input/output and <vector>
for using STL vectors.
Next, we declare an array of vectors named arrayOfVectors
. The declaration std::vector<int> arrayOfVectors[5];
creates an array of size 5. Each element of the array is a std::vector<int>
, meaning it can store a dynamically sized collection of integers.
We then populate the vectors within the array. arrayOfVectors[0].push_back(10);
adds the integer 10 to the vector at index 0 of the array. Similar operations are performed for other elements and vectors.
Finally, a nested loop iterates through the array and the vectors. The outer loop iterates through each element of the array, and the inner loop iterates through each element of the vector at that index. The values are then printed to the console.
Complexity Analysis
Time Complexity: Accessing a particular vector in the array takes O(1) time. Adding elements to the vectors using push_back()
typically takes O(1) amortized time. Iterating through all elements of all vectors depends on the total number of elements stored. If there are 'n' elements across all vectors, the iteration takes O(n) time.
Space Complexity: The space complexity depends on the total number of elements stored in the vectors. If there are 'n' elements stored across all vectors, the space complexity is O(n). The array itself takes O(size), where size is the number of vectors in the array.
Alternative Approaches
Instead of using a fixed-size array, you could use a vector of vectors (`std::vector<std::vector<int>>`). This approach offers more flexibility as the number of vectors can be dynamically changed. However, accessing a specific vector might be slightly slower due to the extra level of indirection compared to accessing an element in a statically sized array.
Conclusion
Arrays of vectors in C++ offer a versatile way to manage collections of data. They provide a structure where each element of the array is a dynamic vector capable of storing a variable number of elements. Understanding their implementation, complexity, and alternative approaches allows you to effectively use them in various programming scenarios.