How to create array of strings in C++

In this tutorial, we are going to learn about How to create array of strings in C++. We will create a fixed-sized and dynamic array with C++ programs examples.

Method to create array of strings in C++


  • Simple 2D char array.
  • C++ string class
  • STL Array container method to Create string array.
  • STL Vector container method to Create string array.

1. 2D char array to create array of strings in C++


In this example, we are creating a character 2D array that will store 4 strings of each with a max length of 20 characters. Here we are using a hard-coded approach and this size or length can not be changed at runtime. This is one limitation of this approach.

  • The first step is to declare this character array with the name chr_arr.
  • We are specifying the indexes as 4 and 20 which means we can store 4 strings and each string can max 20 characters.
  • The next step here is to initialize the values in our array. so we are just assigning 4 strings to this array.

We can not exceed the size of 20 otherwise the rest of the part will be chopped off. Also, we can not assign more names as our size is only 4. If we try to do so at runtime or in code, it will crash our application.

So we have the array ready, now let us print this with the help of for loop by iterating over each element of this array.

C++ Program Example

#include <iostream>

using namespace std;

#include<string>


int main() {

    char chr_arr[4][20] = { "Sachin", "Dravid",
                             "Gavaskar",  "Ganguly" };
  cout << "The name in the array string are:\n"; 
    for (auto &i : chr_arr)
   {
        cout<< i <<endl;
    }
    cout << endl;

    return 0;
}

output

The name in the array string are:
Sachin
Dravid
Gavaskar
Ganguly

2. C++ string class to create array of strings in C++


This method is similar to the one we learned above. Here we are going to make use of the string class of C++. We will create an object of string class with size 4. Then using the initializer list we will assign the strings to it.

We can iterate on these objects to print the values of strings they hold. let us understand this with the help of the below example.

C++ Program Example

#include <iostream>

using namespace std;

#include<string>


int main() {

    string str_arr[4] = { "Sachin", "Dravid",
                             "Gavaskar",  "Ganguly" };
cout << "The name in the array string are:"<<endl;
    for (auto &i : str_arr)
{
        cout<< i <<endl;
    }
    cout << endl;

    return 0;
}

Output

The name in the array string are:
Sachin
Dravid
Gavaskar
Ganguly

3. STL Array container to Create string array in C++


In this method, we will use the array containers of the STL library. Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.

These arrays are similar to the C-style arrays in terms of memory footprint efficiency and provide the common member functions for accessibility. Internally, an array does not keep any data other than the elements it contains (not even its size, which is a template parameter, fixed on compile-time). Let us understand with the help of an example.

C++ Program Example

#include<iostream>
#include<string>
#include<array>

using namespace std;

int main() {
    array<string, 4> str_arr2 = { "Sachin", "Dravid",
  
                             "Gavaskar",  "Ganguly" };
    cout << "The name in the array string are:" <<endl;

for (auto &i : str_arr2)
{
        cout << i <<endl;
    }

    cout << endl;

    return 0;
}

Output

The name in the array string are:
Sachin
Dravid
Gavaskar
Ganguly

4. Vector class to create dynamic array of strings


The methods that we have learned so far use fix size. We were not able to expand the size at runtime. But this next method will give us the flexibility to modify the size at runtime and it can grow as per user need. so let us jump to our next method which is by using vectors.

Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted at the runtime, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end.

Let us understand with the help of an example how can we make a string array with the help of vectors.

#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main() {
   
vector<string> str_arr = { "Sachin", "Dravid",
                             "Gavaskar",  "Ganguly" };

   
    cout << "The name in the array string are:"<<endl ;
for (const auto &it : str_arr)
{
cout << it << ",";
       
    }
    cout << endl;

    str_arr.push_back("Virat");
    str_arr.push_back("Rohit");

    cout << "The New names in the array string are:"<<endl;
    for (const auto &itt : str_arr)
{
cout << itt <<",";
       
    }
    cout << endl;

    return 0;
}

Output

The name in the array string are:
Sachin,Dravid,Gavaskar,Ganguly,
The New names in the array string are:
Sachin,Dravid,Gavaskar,Ganguly,Virat,Rohit,

Summary

In this post, we have learned 4 different ways of How to create an array of strings in C++

  • 2D char array to create array of strings in C++
  • C++ string class to create array of strings in C++
  • STL Array container to Create string array in C++
  • Vector class to create dynaic array of strings