Array of Strings in C++
A String Array in C++ is a collection of strings, stored in an array data structure.
In this tutorial, we will go in-depth into the representation and implementation of array of strings in C++. So, without further ado, let’s start exploring array of strings in C++ in detail.
We will be covering the following sections:
What are Arrays of Strings in C++?
In C++, an array of strings is an array of arrays of characters (i.e., a two-dimensional array of characters) that stores a set of strings. Each element of the array is a null-terminated string. The size of the array is fixed, meaning it is determined at the time of declaration and cannot be changed later on. The elements of the array can be accessed using their index, just like any other array.
You can also explore: Erasing Elements from a Vector in C++
Let’s take a look at an example:
string words[5] = {"hello", "this", "is", "Naukri", "Learning"}; cout << words[0] << endl; //prints "hello"
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Methods to Create an Array of Strings in C++
In C++, there are several methods to create an array of strings. These include:
- Utilizing pointers to dynamically allocate memory for the array
- Creating a two-dimensional array of characters
- Using the built-in string class to store the strings
- Using the vector container class to store and manipulate the strings
- Using the array class, a container class that is introduced in C++11, which is a fixed-size array of elements.
Let’s look at how to implement each of the above-stated methods.
You can also explore: Multiple inheritance in C++ with real life analogy
Using Pointers
In C++, pointers are used to store the memory addresses of variables. By using pointers, an array of string literals can be created by creating an array of pointers. Each element of the array is a pointer that points to the first value of the corresponding string in the array. This pointer will always point to the first value of the string until the array is traversed.
Here is an example of a C++ program that demonstrates the use of pointers to create an array of strings:
#include <iostream> using namespace std; int main() { char *words[5] = {"Hi", "Welcome", "to", "Naukri", "Learning"}; for (int i = 0; i < 5; i++) { cout << words[i] << endl; } return 0; }
Output:
In this program, an array of pointers to characters is declared and initialized with a set of string literals. The pointers in the array will point to the first character of each string in the array. The for loop is used to iterate through the array and print out the strings.
You can also explore: How to use For Loop in C++
It is worth noting that this way of creating an array of strings can also be achieved using dynamic memory allocation. But that would require a different approach and is a more advanced topic.
You can also explore: Understanding Copy Constructor in C++
Using 2D Array
A 2-D array, also known as a two-dimensional array. It is a simple form of a multidimensional array that stores data in a tabular format. This method is useful when the length of all the strings is known and a specific amount of memory is required. With this method, the space for the strings is allocated in a single block. Allowing for easy access to each element of the array using its indices.
Here is an example of a C++ program that demonstrates the use of a 2D array to create an array of strings:
#include <iostream> using namespace std; int main() { char words[5][10] = {"Hi", "Welcome", "to", "Naukri", "Learning"}; for (int i = 0; i < 5; i++) { cout << words[i] << endl; } return 0; }
Output:
In this program, a two-dimensional array of characters is declared and initialized with a set of string literals. The first dimension of the array is 5, representing the number of strings in the array and the second dimension is 10, representing the maximum length of each string. The for loop is used to iterate through the array and print out the strings.
You can also explore: What is Static Data Member in C++
Please keep in mind that the size of the 2-D array must be known beforehand and cannot be changed later. This also applies to the size of the second dimension, which in this case is set to 10, so the strings can’t be longer than 10 characters. If they are, they will be truncated.
Using String Class
The Standard Template Library (STL) string class, also known as std::string. It can be used to create an array of strings that can be modified. Unlike arrays of pointers or 2-D arrays, the size of the strings in the array is not fixed and can be changed at runtime. This makes the array dynamic in nature. The std::string class provides a variety of in-built functions that can be used to create and manipulate the array of strings.
Here is an example of a C++ program that demonstrates the use of string class to create an array of strings:
#include <iostream> #include <string> using namespace std; int main() { string words[5] = {"C++", "Programming", "sure", "is", "fun"}; for (int i = 0; i < 5; i++) { cout << words[i] << endl; } return 0; }
Output:
In this program, an array of string objects is declared and initialized with a set of string literals. You can use the for loop to iterate through the array and print out the strings.
You can also explore: Convert char to int in C++ Using Different Methods
The string class provides a variety of in-built functions that can be used to manipulate the strings in the array, such as length(), substr(), find(), and replace(). Additionally, the string class also provides the ability to concatenate strings and extract substrings, which is not possible with C-style strings.
Using Vector Class
A vector is a dynamic array that automatically increases its size as new elements are added. The Standard Template Library (STL) vector container class can be used to create an array of strings that can grow or shrink in size as needed. This method is specific to C++, as C does not have classes.
Note that the initializer-list syntax used in the example code requires a compiler that supports the 2011 C++ standard or later version, and not all compilers may have this feature. It is important to check the version of your compiler to ensure compatibility.
Let’s look at an example C++ program that demonstrates the use of vector class to create an array of strings:
#include <iostream> #include <vector> using namespace std; int main() { vector<string> words = {"Learn", "C++", "with", "Naukri", "Learning"}; for (int i = 0; i < words.size(); i++) { cout << words[i] << endl; } return 0; }
Output:
In the above program, an object of vector<string> is declared and initialized with a set of string literals using the initializer-list syntax. You can use the for loop to iterate through the vector and print out the strings.
The vector class provides a variety of member functions that can be used to manipulate the vector, such as push_back(), pop_back(), size(), empty(), clear(), and resize(). You can also use begin() and end() to get the iterator pointing to the first and last element respectively.
This method of creating an array of strings is dynamic in nature. This means that you can add or remove elements to the array as needed, without having to specify the size beforehand.
You can also explore: Understanding Break Statement in C++
Using Array Class
An array is a data structure that stores a fixed-size collection of elements, all of the same type, in contiguous memory locations. The Standard Template Library (STL) array container class can be used to create a fixed-size array of strings. The array class is similar to the vector class, but the size of the array is fixed and you cannot modify it at runtime.
Let’s look at an example C++ program that demonstrates the use of array class to create an array of strings:
#include <iostream> #include <array>using namespace std; int main() { array<string, 5> words = {"array", "of", "string", "in", "CPP"}; for (int i = 0; i < words.size(); i++) { cout << words[i] << endl; } return 0; }
Output:
In the above program, an object of array<string, 5> is declared and initialized with a set of string literals using the initializer-list syntax. The array class is a template class. The first template parameter string is the type of elements it will store and the second template parameter 5 is the number of elements the array will hold. You can use the for loop to iterate through the array and print out the strings.
The array class provides a variety of member functions that can be used to manipulate the array, such as size(), at(), fill(), begin(), and end(). It also provides the subscript operator [] for accessing its elements.
This method of creating an array of strings is fixed in nature. This means that you must specify the size of the array at the time of declaration as you cannot modify it later.
You can also explore: Function Overloading in C++
Endnotes
In this article, we have discussed the different ways to create an array of strings in C++. We have discussed the use of pointers, 2-D arrays, the string class, the vector class, and the array class to create and manipulate arrays of strings. Each method has its own set of advantages and limitations, and the choice of which one to use depends on the specific requirements of the program.
If you want to learn more about C++ and solidify your basics, you can explore our articles on C++.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio