To create a vector of empty vectors in C++, you can initialize a vector and populate it with empty vectors using a for loop.
In this example,
header to use the std::vector
class.empty_vector_list
of type std::vector>
.empty_vector_list
now contains the desired number of empty vectors.#include <vector>
#include <iostream>
int main() {
std::vector<std::vector<int>> empty_vector_list;
int num_empty_vectors = 5;
for (int i = 0; i < num_empty_vectors; ++i) {
empty_vector_list.push_back(std::vector<int>());
}
for (const auto& vec : empty_vector_list) {
for (int val : vec) {
std::cout << val << " ";
}
std::cout << std::endl;
}
return 0;
}
In this example,
header to use the std::vector
class.std::vector>
syntax to define a vector of empty vectors and initialize it with an empty initializer list {}
.empty_vector_list
is automatically populated with the desired number of empty vectors.#include <vector>
#include <iostream>
int main() {
std::vector<std::vector<int>> empty_vector_list(5);
for (const auto& vec : empty_vector_list) {
for (int val : vec) {
std::cout << val << " ";
}
std::cout << std::endl;
}
return 0;
}
In this tutorial, we learned How to create a Vector of Empty Vectors in C++ language with well detailed examples.