Tutorials  /  Linux Basics

Understanding C++ String Array

Ccentron Redaktion · January 2024 ·5 min read ·Linux Basics, Tutorial

Hey, Folks! So, as programmers, we often deal with Arrays of all data types. We’ll cover C++ string array in today’s article.

Ways to declare a C++ String Array

1. The String keyword to Create String Array in C++

C++ provides us with ‘string’ keyword to declare and manipulate data in a String array.

The string keyword allocates memory to the elements of the array at dynamic or run-time accordingly. Thus it saves the headache of static memory allocation of data-elements.

Syntax: To declare an array of string using ‘string’ keyword

Code
string array-name;

Further, we can initialize the array of string using the below syntax:

Code
string array-name={'val1','val2',.....,'valN'};

Example 1:

Code
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{
	string fruits[5] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" }; 

	cout<<"String array:\n";
	for (int x = 0; x< 5; x++) 
		cout << fruits[x] << "\n"; 
} 

In the above example, we have initialized the string array and have used C++ for loops to traverse through the array and print the data items present in the string array.

Output:

Code
String array:
Grapes
Apple
Pineapple
Banana
Jackfruit

Example 2:

Code
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
 
	string arr[5];
	cout<<"Enter the elements:"<<endl; for(int x = 0; x<5;x++) { cin>>arr[x];
	}

	cout<<"\nString array:\n";
	for (int x = 0; x< 5; x++) 
		cout << arr[x] << "\n"; 
} 

As you all can witness, in the above example, we did accept the data items of the string array from the console i.e. user input is taken and we have printed the elements of the string array.

Output:

Code
Enter the elements:
Jim
Nick
Daisy
Joha
Sam

String array:
Jim
Nick
Daisy
Joha
Sam
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

2. Using C++ STL Container - Vector

C++ Standard Template Library provides us with containers to work with data and store it efficiently.

Vector, being one such container, stores the array elements in a dynamic manner. Thus, C++ Vectors can be used to create a string array and manipulate the same easily.

Syntax:

Code
vector<string> array-name;

The vector.push_back(element) method is used to add elements to the vector string array.

The vector.size() method is used to calculate the length of the array i.e. the count of the elements input to the string array.

Example:

Code
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	vector<string> arr; 
	arr.push_back("Ace"); 
	arr.push_back("King"); 
	arr.push_back("Queen"); 

	int size = arr.size();
	cout << "Elements of the vector array:" << endl;
	for (int x= 0; x< size; x++) 
		cout << arr[x] << "\n"; 
} 

Output:

Code
Elements of the vector array:
Ace
King
Queen

3. Using 2D char array

A 2D array represents an array of string in C++. So, we can use a 2D char array to represent string type elements in an array.

The char array creates and stores elements at static or compile-time i.e. the number and size of elements stay fixed/constant.

Syntax:

Code
char array-name[number-of-items][maximun_size-of-string];

Example:

Code
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	char fruits[5][10] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" }; 

	cout << "Character array:" << endl;
	for (int x = 0; x< 5; x++) 
		cout << fruits[x] << "\n"; 
} 

In the above snippet of code, we have created a char array to store string type elements. i.e. char array[5][10]. Here 5 depicts the count of string elements and 10 points to the maximum size of the input string.

Output:

Code
Character array:
Grapes
Apple
Pineapple
Banana
Jackfruit

C++ String Array as an Argument to a Function

A string array can also be passed to a function as an argument the same way as another non-string type array is passed to it.

Syntax:

Code
return-type function-name(string array-name)
{
  // body of the function
}

Example:

Code
#include <iostream>
#include <string>
using namespace std;
void show(string arr[4]){
   
for(int x=0;x<4;x++)
{
    cout<<arr[x]<<endl;
}

}
int main() {
string arr[4] = {"Jim", "Jeo", "Jio", "John"};
cout<<"Printing elements of the string array:"<<endl;
show(arr);

}

Output:

Code
Printing elements of the string array:
Jim
Jeo
Jio
John

Conclusion

In this article, we have understood ways to create a string arrays and techniques to use it in a function. Understanding C++ String Array

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren