How to Initialize Vector of Char Arrays in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {“apple”, “banana”, “cherry”}Initializing a Vector of Char Arrays in C++ We cannot directly store the char array into the vector, but we can store them as pointers. To initialize a vector of char arrays in C++, we can use the list initialization where each element is a string literal. The pointer to this string literal is then stored inside the vector of char arrays. Note: It is strongly recommended to avoid using array or pointer to arrays as the data type of the vector. C++ Program to Initialize a Vector of Char Arrays C++ // C++ Program to illustrate how to initialize a vector of // char arrays #include <iostream> #include <vector> using namespace std; // Driver Code int main() { // Creating a vector of char arrays vector<const char*> myVector = { "apple", "banana", "cherry" }; // Displaying the vector elements for (int i = 0; i < myVector.size(); ++i) { cout << myVector[i] << endl; } return 0; } Outputapple banana cherry Time Complexity: O(N), where N is the number of char arrays. Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Initialize Vector of Char Arrays in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ STL cpp-string cpp-array cpp-vector CPP Examples +3 More Practice Tags : CPPSTL Similar Reads How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i 3 min read How to Initialize 3D Vector in C++ STL? Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This me 4 min read How to Create a Vector of Arrays in C++? 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 2 min read How to Initialize a Deque from a Vector in C++? In C++, the Standard Template Library(STL) provides a container deque also known as a double-ended queue where insertion and deletions are possible from both ends. On the other hand, vectors are dynamic containers that can resize themselves during the insertion or deletion of elements. In this artic 2 min read How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look 2 min read How to Initialize a Dynamic Array in C++? In C++, dynamic arrays allow users to allocate memory dynamically. They are useful when the size of the array is not known at compile time. In this article, we will look at how to initialize a dynamic array in C++. Initializing Dynamic Arrays in C++The dynamic arrays can be initialized at the time o 1 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 Convert Char Array to Int in C++ In C++, a character array is treated as a sequence of characters also known as a string. Converting a character array into an integer is a common task that can be performed using various methods and in this article, we will learn how to convert char array to int in C++. Example Input:char arr1[] ="1 2 min read How to Initialize a Set with an Array in C++? In C++, an array stores data in contiguous memory locations and can have duplicates as well whereas a set stores unique elements only in a sorted order. In this article, we will learn how to initialize a set with an array in C++. For Example, Input:arr[]={2, 1, 5, 4, 3, 5};Output:Element in Set are: 2 min read Like