Create an Array of Strings in C++

This tutorial will discuss multiple ways to create an array of strings in C++.

Table Of Contents

Create an Array of std::string in C++

To create an array of strings in C++, we can either use the std::string as the data type of the array while declaring it. In the line below, we create an array of strings that contains 5 string values:

// Create an array of strings
std::string arr[] = {
                "London",
                "Tokyo",
                "Las vegas",
                "New York",
                "Sydney"};

We can determine the size of this array using the sizeof operator like this:

// Get the size of array
size_t len = sizeof(arr) / sizeof(arr[0]);

Afterward, we can iterate over the array to print each string element.

// Access and print strings in array
for (int i = 0; i < len; ++i)
{
    std::cout << "arr["
                << i
                << "] = "
                << arr[i]
                << std::endl;
}

Let’s see the complete example,

#include <iostream>
#include <string>

int main()
{
    // Create an array of strings
    std::string arr[] = {
                    "London",
                    "Tokyo",
                    "Las vegas",
                    "New York",
                    "Sydney"};

    // Get the size of array
    size_t len = sizeof(arr) / sizeof(arr[0]);

    // Access and print strings in array
    for (int i = 0; i < len; ++i)
    {
        std::cout << "arr["
                  << i
                  << "] = "
                  << arr[i]
                  << std::endl;
    }

    return 0;
}

Output

arr[0] = London
arr[1] = Tokyo
arr[2] = Las vegas
arr[3] = New York
arr[4] = Sydney

Create an Array of const char* in C++

If you are using a const char* instead of std::string, you can also declare an array of strings using the const char*. This array would contain 5 string elements, and the data type of the array would be a const chat pointer.

const char* strArr[] = {
                "London",
                "Tokyo",
                "Las vegas",
                "New York",
                "Sydney"};

We can determine the size of this array using the sizeof operator like this:

// Get the size of array
size_t arrayLen = sizeof(strArr) / sizeof(strArr[0]);

Now, we will iterate over each string value in the array and print it to the console.

// Access and print strings in array
for (int i = 0; i < arrayLen; ++i)
{
    std::cout << "strArr["
                << i
                << "] = "
                << strArr[i]
                << std::endl;
}

Let’s see the complete example,

#include <iostream>

int main()
{
    // Create an array of char pointers (array of C-style strings)
    const char* strArr[] = {
                    "London",
                    "Tokyo",
                    "Las vegas",
                    "New York",
                    "Sydney"};

    // Get the size of array
    size_t arrayLen = sizeof(strArr) / sizeof(strArr[0]);

    // Access and print strings in array
    for (int i = 0; i < arrayLen; ++i)
    {
        std::cout << "strArr["
                  << i
                  << "] = "
                  << strArr[i]
                  << std::endl;
    }

    return 0;
}

Output

strArr[0] = London
strArr[1] = Tokyo
strArr[2] = Las vegas
strArr[3] = New York
strArr[4] = Sydney

Summary

We learned about two ways to create an array of string in C++.

Summary

Today, we learned about multiple ways to create an array of strings in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top