c+字符串数组_了解C ++字符串数组

c+字符串数组

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

嘿伙计! 因此,作为程序员,我们经常处理所有数据类型的数组。 我们将在今天的文章中介绍C ++字符串数组

声明C ++字符串数组的方法 (Ways to declare an C++ String Array)

Ways To Create C++ String Array
Ways To Create C++ String Array 创建C ++字符串数组的方法


1.使用String关键字在C ++中创建字符串数组 (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.

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

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.

string keyword相应地在动态或运行时将内存分配给数组的元素。 这样就免除了静态分配数据元素的麻烦。

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

语法:使用'string'关键字声明一个字符串数组


string array-name[size];

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

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


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

Example 1:

范例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"; 
} 

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.

在上面的示例中,我们初始化了字符串数组,并使用C ++进行循环遍历该数组并打印出字符串数组中存在的数据项。

Output:

输出:


String array:
Grapes
Apple
Pineapple
Banana
Jackfruit

Example 2:

范例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"; 
} 

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:

输出:


Enter the elements:
Jim
Nick
Daisy
Joha
Sam

String array:
Jim
Nick
Daisy
Joha
Sam


2.使用C ++ STL容器–矢量 (2. Using C++ STL Container – Vector)

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

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

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.

向量是一个这样的容器,它以动态方式存储数组元素。 因此, C ++向量可用于创建字符串数组并轻松对其进行操作。

Syntax:

句法:


vector<string>array-name;
  • The vector.push_back(element) method is used to add elements to the vector string array.

    vector.push_back(element)方法用于将元素添加到向量字符串数组。
  • 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.

    vector.size()方法用于计算数组的长度,即输入到字符串数组的元素的数量。

Example:

例:


#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:

输出:


Elements of the vector array:
Ace
King
Queen


3.使用2D char数组 (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.

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

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

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

Syntax:

句法:


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

Example:

例:


#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"; 
} 

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.

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

Output:

输出


Character array:
Grapes
Apple
Pineapple
Banana
Jackfruit


C ++字符串数组作为函数的参数 (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:

句法:


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

Example:

例:


#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:

输出:


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.

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

参考资料 (References)

翻译自: https://www.journaldev.com/37614/string-array-in-c-plus-plus

c+字符串数组

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值