嘿,大家好!作为程序员,我们经常处理各种数据类型的数组。在今天的文章中,我们将介绍C++字符串数组。
声明C++字符串数组的方法

1. 使用String关键字在C++中创建字符串数组
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 容器 – 向量(Vector)
C++ Standard Template Library provides us with containers to work with data and store it efficiently.
向量(Vector)是一種容器,以動態方式存儲數組元素。因此,可以使用 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. 使用 2D 字符數組
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 array)在靜態或編譯時創建和存儲元素,即元素的數量和大小保持 固定/恆定。
語法:
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";
}
在上面的程式碼片段中,我們已經創建了一個字符數組來存儲字符串類型的元素,即字符數組[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