Array of Strings in C++ (3 Different Ways to Create)
Array of Strings can be created in C++, which can be quite handy. There are 3 ways to create Array of Strings.
-
Using 2D array (Both C and C++): This method is useful for shuffling, comparing and accessing characters randomly.
Syntax:Char “Name” [“Number of Strings”][“MaxSize of String”] Example: Char colour [4][10] // Here 4 colours can be inserted with max String size of 10.
// C++ program to demonstrate array of strings using// 2D character array#include<bits/stdc++.h>usingnamespacestd;intmain(){// Initialize 2D arraycharcolour[4][10] = {"Blue","Red","Orange","Yellow"};// Printing Strings stored in 2D arrayfor(inti = 0; i < 4; i++)cout << colour[i] <<"\n";return0;}chevron_rightfilter_noneDrawbacks of this method:
- Number of Strings and Size of String – both the values are fixed.
- A 2D array is allocated, whose second dimension is equal to maximum sized string which causes wastage of space.
- Using string Keyword (Only in C++): In this method, size of the string is not fixed, hence space is saved.
Syntax:String “Name” [“Number of Strings”] Example: String colour[4]
// C++ program to demonstrate array of strings using// array of strings.#include<bits/stdc++.h>usingnamespacestd;intmain(){// Initialize String Arraystring colour[4] = {"Blue","Red","Orange","Yellow"};// Print Stringsfor(inti = 0; i < 4; i++)cout << colour[i] <<"\n";}chevron_rightfilter_noneDrawback of this method:
- Array is of fixed Size
- Using Vectors (Only C++) STL Container Vector can be used to dynamically allocate Array.
Syntax:Vector “Name” Example: Vector Colour
// C++ program to demonstrate vector of strings using#include<bits/stdc++.h>usingnamespacestd;intmain(){// Declaring Vector of String typevector <string> colour;// Initialize vector with strings using push_back// commandcolour.push_back("Blue");colour.push_back("Red");colour.push_back("Orange");colour.push_back("Yellow");// Print Strings stored in Vectorfor(inti=0; i<colour.size(); i++)cout << colour[i] <<"\n";}chevron_rightfilter_noneOutput:
Blue Red Orange Yellow
Out of all the three methods, Vector seem to be the best way for creating array of Strings in C++.
This article is contributed by Kartik Ahuja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Count ways to increase LCS length of two strings by one
- Search in an array of strings where non-empty strings are sorted
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Create linked list from a given array
- How to create a dynamic 2D array inside a class in C++ ?
- Create a Sorted Array Using Binary Search
- Count of strings in the first array which are smaller than every string in the second array
- Ways to split array into two groups of same XOR value
- Ways to sum to N using array elements with repetition allowed
- Number of ways to divide an array into K equal sum sub-arrays
- Count number of ways to divide an array into two halves with same sum
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Number of ways to choose elements from the array such that their average is K
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array



