
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Array of Strings
A string is a sequence of characters used to store information as text. In C++, strings are represented as array of characters using the std::string class. In this article, we will learn what is an array of strings in C++, how to declare and initialize it, and how to access its elements.
C++ Array of Strings
Array of strings refer to an array where each element is a string. Just like a normal array, we can define an array of strings by specifying the data type as std::string and the size of the array inside square brackets. To initialize an array of strings, we can use curly braces to provide the string literals.
// Declare and initialize an array of strings string arr[SIZE] = {"string1", "string2", "string3", ...}; // Access strings cout << arr[0]; // Output: string1 cout << arr[3]; // Output: string4
In the above code, we declared an array of strings named arr with a size of SIZE. We initialized the first five elements with string literals. We then accessed and printed the first and fourth elements of the array using cout.
Example of Array of Strings in C++
Here is a simple example that demonstrates how to declare, initialize, and access an array of strings in C++.
#include <iostream> #include <string> using namespace std; int main() { // Declaration and initialization of an array of strings string arr[5] = {"Hello", "World", "C++", "Array", "Strings"}; // Accessing and printing elements of the array for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
The output of the above code will be:
Hello World C++ Array Strings
Array of Strings Using Pointer
Pointers are variables that store the address of another variable. You can use pointers to store an array of strings. Following is the syntax to declare and initialize an array of strings using pointers:
string* arr = new string[5]; arr[0] = "Hello";
In the above code, we declared a pointer arr that points to an array of strings. We then initialized the first element of the array with the string "Hello". You can access the elements of the array using the pointer just like you would with a normal array.
Example
The example code demonstrates how to use pointers to create and manage an array of strings.
#include <iostream> #include <string> using namespace std; int main() { // Declaration and initialization of an array of strings using pointers string* arr = new string[5]; arr[0] = "Hello"; arr[1] = "World"; arr[2] = "C++"; arr[3] = "Array"; arr[4] = "Strings"; // Accessing and printing elements of the array for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; // Deallocating memory delete[] arr; return 0; }
The output of the above code will be:
Hello World C++ Array Strings
C-style Array of Strings
C++ also supports C-style arrays of strings, which are basically 2D array of characters. But this approach wastes more memory and is less flexible compared to other methods. Below is an example of how to use a C-style array of strings:
Example
In the code below, we declare a C-style array of strings and print its elements.
#include <iostream> using namespace std; int main() { // Declaration and initialization of a C-style array of strings char arr[5][10] = {"Hello", "World", "C++", "Array", "Strings"}; // Accessing and printing elements of the array for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
The output of the above code will be:
Hello World C++ Array Strings
Vector of Strings
Instead of using array of strings, you can also use a vector of strings in C++. A vector is a dynamic array that can grow or shrink in size. This can be used when you are not sure about the number of strings you will need. The example below shows how to use a vector to store an array of strings:
Example
In the code below, you can see that the size of vector is not fixed, and we are able to add more strings dynamically using the push_back method.
#include <iostream> #include <vector> #include <string> using namespace std; int main() { // Declaration and initialization of a vector of strings vector<string> vec = {"Hello", "World", "C++", "Array", "Strings"}; // Adding more strings to the vector vec.push_back("Using"); vec.push_back("Vector"); // Accessing and printing elements of the vector for (const auto& str : vec) { cout << str << " "; } cout << endl; return 0; }
The output of the above code will be:
Hello World C++ Array Strings Using Vector