了解 C++ 字符串数组

所以,作为程序员,我们经常处理所有数据类型的数组,我们将在今天的文章中涵盖 ** C++ 字符串数组。

如何声明 C++ 字符串数列

Ways To Create C++ String Array


1. C++ 中创建 String Array 的关键字

C++为我们提供**string**的关键字,以声明和操纵 **String数组中的数据。

字符串关键字将内存分配给相应的 动态或运行时间的数组元素,从而节省了数据元素的静态内存分配的头痛。

Syntax:使用string关键字声明一系列字符串

1string array-name[size];

此外,我们可以使用下面的语法初始化字符串的数组:

1string array-name[size]={'val1','val2',.....,'valN'};

例子一:**

 1#include <bits/stdc++.h> 
 2using namespace std; 
 3
 4int main() 
 5{
 6    string fruits[5] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" }; 
 7
 8    cout<<"String array:\n";
 9    for (int x = 0; x< 5; x++) 
10    	cout << fruits[x] << "\n"; 
11}

在上面的示例中,我们已经初始化了字符串数组,并使用 C++ for loop 来穿越数组并打印在字符串数组中存在的数据项目。

出发点:**

1String array:
2Grapes
3Apple
4Pineapple
5Banana
6Jackfruit

例子二:

 1#include <bits/stdc++.h> 
 2using namespace std; 
 3
 4int main() 
 5{ 
 6
 7    string arr[5];
 8    cout<<"Enter the elements:"<<endl;
 9    for(int x = 0; x<5;x++)
10    {
11        cin>>arr[x];
12    }
13
14    cout<<"\nString array:\n";
15    for (int x = 0; x< 5; x++) 
16    	cout << arr[x] << "\n"; 
17}

正如您所见,在上面的示例中,我们确实从控制台接收了字符串数组的数据项目,即用户输入,我们打印了字符串数组的元素。

出发点:**

 1Enter the elements:
 2Jim
 3Nick
 4Daisy
 5Joha
 6Sam
 7
 8String array:
 9Jim
10Nick
11Daisy
12Joha
13Sam

使用 C++ STL 容器 - 矢量

C++标准模板库为我们提供容器来处理数据并有效地存储数据。

因此, C++ 矢量可以用来创建一个字符串数组并轻松地操纵它。

合成:**

1vector<string>array-name;
  • 使用 vector.push_back(element) 方法将元素添加到矢量字符串数组中.
  • 使用 vector.size() 方法来计算数组的长度,即输入字符串数组中的元素数。

** 例子:**

 1#include <bits/stdc++.h> 
 2using namespace std; 
 3
 4int main() 
 5{ 
 6
 7    vector<string> arr; 
 8    arr.push_back("Ace"); 
 9    arr.push_back("King"); 
10    arr.push_back("Queen"); 
11
12       int size = arr.size();
13cout<<"Elements of the vector array:"<<endl;
14    for (int x= 0; x< size; x++) 
15    	cout << arr[x] << "\n"; 
16}

出发点:**

1Elements of the vector array:
2Ace
3King
4Queen

使用2D Char Array

2D 数组代表了 C++ 中的一个字符串数组,因此,我们可以使用 2D 字符串数组来表示数组中的字符串类型元素。

**char 数组以静态或编译时间创建和存储元素,即元素的数量和大小保持 固定/恒定

合成:**

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

** 例子:**

 1#include <bits/stdc++.h> 
 2using namespace std; 
 3
 4int main() 
 5{ 
 6
 7    char fruits[5][10] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" }; 
 8
 9    cout<<"Character array:\n";
10    for (int x = 0; x< 5; x++) 
11    	cout << fruits[x] << "\n"; 
12}

在上面的代码片段中,我们创建了一个 char 数组来存储字符串类型元素,即 char 数组[5][10]. 在这里,5 表示字符串元素的数目,10 点表示输入字符串的最大大小。

出发点:

1Character array:
2Grapes
3Apple
4Pineapple
5Banana
6Jackfruit

C++ String Array 作为函数的参数

一个字符串数列也可以作为一个字符串数组作为一个字符串数组作为一个字符串数组作为一个字符串数组作为一个字符串数组作为一个字符串数组作为一个字符串数组。

合成:**

1return-type function-name(string array-name[size])
2{
3  // body of the function
4}

** 例子:**

 1#include <iostream>
 2#include<string>
 3using namespace std;
 4void show(string arr[4]){
 5
 6for(int x=0;x<4;x++)
 7{
 8    cout<<arr[x]<<endl;
 9}
10
11}
12int main() {
13string arr[4] = {"Jim", "Jeo", "Jio", "John"};
14cout<<"Printing elements of the string array:"<<endl;
15show(arr);
16
17}

出发点:**

1Printing elements of the string array:
2Jim
3Jeo
4Jio
5John

结论

在本文中,我们了解了创建字符串数组的方法和技术,以便在函数中使用它。

参考

Published At
Categories with 技术
Tagged with
comments powered by Disqus