みなさん、こんにちは!プログラマーとして、私たちはよくさまざまなデータ型の配列を扱います。今日の記事では、C++の文字列配列について説明します。
C++の文字列配列を宣言する方法

1. C++で文字列配列を作成するためのstringキーワード
C++ provides us with ‘string’ keyword to declare and manipulate data in a String array.
stringキーワード
は、動的または実行時に配列の要素にメモリを割り当てます。したがって、データ要素の静的なメモリ割り当ての手間を省くことができます。
構文:stringキーワードを使用して文字列の配列を宣言する
string array-name[size];
さらに、以下の構文を使用して文字列の配列を初期化することができます:
string array-name[size]={'val1','val2',.....,'valN'};
例1:
#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";
}
上記の例では、文字列の配列を初期化し、C++のループを使用して配列をトラバースし、文字列のデータアイテムを表示しています。
出力:
String array:
Grapes
Apple
Pineapple
Banana
Jackfruit
例2:
#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";
}
上記の例では、文字列の配列のデータアイテムをコンソールから受け入れ、ユーザーの入力を取得し、文字列の配列の要素を表示しています。
出力:
Enter the elements:
Jim
Nick
Daisy
Joha
Sam
String array:
Jim
Nick
Daisy
Joha
Sam
2. C++ STLコンテナ – ベクトルの使用
C++ Standard Template Library provides us with containers to work with data and store it efficiently.
ベクトルは、要素を動的に格納するためのコンテナの一つです。そのため、 C++ベクトルを使用して文字列の配列を作成し、簡単に操作することができます。
構文:
vector<string>array-name;
vector.push_back(element)
メソッドは、ベクトルの文字列配列に要素を追加するために使用されます。vector.size()
メソッドは、配列の長さ、つまり文字列配列に入力された要素の数を計算するために使用されます。
例:
#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";
}
出力:
Elements of the vector array:
Ace
King
Queen
3. 2次元char配列を使用する
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.
char配列は、要素を静的またはコンパイル時に作成し、格納します。つまり、要素の数とサイズは固定/一定です。
構文:
char array-name[number-of-items][maximun_size-of-string];
例:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char fruits[5][10] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" };
cout<<"Character array:\n";
for (int x = 0; x< 5; x++)
cout << fruits[x] << "\n";
}
上記のコードの断片では、文字列型の要素を格納するためにchar配列を作成しました。すなわち、char array[5][10]です。ここで、5は文字列要素の数を示し、10は入力文字列の最大サイズを示しています。
出力:
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.
構文:
return-type function-name(string array-name[size])
{
// 関数の本体
}
例:
#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);
}
出力:
Printing elements of the string array:
Jim
Jeo
Jio
John
結論
この記事では、文字列配列を作成する方法と、関数で使用するための技術について理解しました。
参考文献
Source:
https://www.digitalocean.com/community/tutorials/string-array-in-c-plus-plus