
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Array of Strings
In this section we will see how to define an array of strings in C++. As we know that in C, there was no strings. We have to create strings using character array. So to make some array of strings, we have to make a 2-dimentional array of characters. Each rows are holding different strings in that matrix.
In C++ there is a class called string. Using this class object we can store string type data, and use them very efficiently. We can create array of objects so we can easily create array of strings.
After that we will also see how to make string type vector object and use them as an array.
Example
#include<iostream> using namespace std; int main() { string animals[4] = {"Elephant", "Lion", "Deer", "Tiger"}; //The string type array for (int i = 0; i < 4; i++) cout << animals[i] << endl; }
Output
Elephant Lion Deer Tiger
Now let us see how to create string array using vectors. The vector is available in C++ standard library. It uses dynamically allocated array.
Example
#include<iostream> #include<vector> using namespace std; int main() { vector<string> animal_vec; animal_vec.push_back("Elephant"); animal_vec.push_back("Lion"); animal_vec.push_back("Deer"); animal_vec.push_back("Tiger"); for(int i = 0; i<animal_vec.size(); i++) { cout << animal_vec[i] << endl; } }
Output
Elephant Lion Deer Tiger
- Related Articles
- Array of Strings in C++
- C Program to Reverse Array of Strings
- C Program to Sort an array of names or strings
- Sort an array of strings according to string lengths in C++
- How to convert array of decimal strings to array of integer strings without decimal in JavaScript
- Print all pairs of anagrams in a given array of strings in C++
- C# Program to search for a string in an array of strings
- C program to print array of pointers to strings and their address
- Python - Ways to convert array of strings to array of floats
- Making a Palindrome pair in an array of words (or strings) in C++
- How to convert array of comma separated strings to array of objects?
- Convert an array of datetimes into an array of strings in Python
- How to convert array of strings to array of numbers in JavaScript?
- How to create array of strings in Java?
- Find char combination in array of strings JavaScript

Advertisements