How to Create a Vector of Arrays in C++? Last Updated : 12 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3 = {7, 8, 9}; Output: Vector of Arrays: {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Creating a Vector of Arrays in C++To create a vector of arrays in C++, we can use the std::array container provided by the STL library. The std::array is a container that encapsulates fixed-size arrays. We can then create a vector of these arrays using the following syntax: Syntax to Declare Vector of Arraysvector< array<datatype, size> > vec_name;Here, datatype denotes the type of data you want to store in the arrays.size denotes the size of each array present in the vector.vec_name is the name of the vector of vector.Note: To declare a vector of plain old datatype arrays, we must store them as pointers as the vector elements should be copy constructible and assignable, and arrays is neither of them. C++ Program to Create a Vector of ArraysThe below program demonstrates how we can create a vector of arrays in C++ STL. C++ // C++ Program to illustrate how to create a vector of // arrays #include <array> #include <iostream> #include <vector> using namespace std; int main() { // Initializing a vector of arrays of size 4 vector<array<int, 4> > vec; // Create arrays to insert into the vector array<int, 4> arr1{ { 1, 2, 3, 4 } }; array<int, 4> arr2{ { 6, 7, 8, 9 } }; // Insert the arrays into the vector vec.push_back(arr1); vec.push_back(arr2); // Print the vector of arrays cout << "Elements in a Vector are: " << endl; for (auto& arr : vec) { for (auto& el : arr) cout << el << ' '; } return 0; } OutputElements in a Vector are: 1 2 3 4 6 7 8 9 Time Complexity: O(N), here N is the number of arrays.Auxiliary Space: O(N * M), where M is the average size of the arrays. Comment More infoAdvertise with us Next Article How to Create a Vector of Arrays in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-array cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read How to Create Array of Arrays in C++ Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arra 3 min read How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9}; 2 min read How to Convert an Array to a Vector in C++? In this article, we will learn how to convert the given array into vector in C++.The simplest method to convert an array to vector is by using range constructor of vector. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 6, 2, 9}; 3 min read How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read How to Create an Array of Structs in C++? In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++. Creating an Array of Structs in C++To create an arr 2 min read How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 min read How to Copy a Vector to an Array in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. Sometimes, we may need to copy the contents of a vector to the POD array. In this article, we will learn how to copy a vector to an array in C++. Example: Input: myVec = {10,20,30,40,50,60};Output: array: {10,20 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read Like