C++中的字符串数组(创建5种不同的方法)

在C和C++中,字符串是一维的字符数组,C中的字符串数组是字符的二维数组。有很多方法可以声明它们,这里给出了一些有用的方法。

null

1.使用指针:

我们通过创建指针数组来创建字符串文本数组。

这是由C和C++支持的。

CPP

// C++ program to demonstrate array of strings using
// 2D character array
#include <iostream>
int main()
{
// Initialize array of pointer
const char *colour[4] = { "Blue" , "Red" ,
"Orange" , "Yellow" };
// Printing Strings stored in 2D array
for ( int i = 0; i < 4; i++)
std::cout << colour[i] << "" ;
return 0;
}


输出:

BlueRedOrangeYellow

  • 字符串的数量是固定的,但不必固定。4可以省略,编译器将计算正确的大小。
  • 这些字符串是常量,其内容无法更改。因为字符串文字(字面意思是带引号的字符串)存在于内存的只读区域中,所以我们必须在这里指定“const”,以防止可能导致程序崩溃的不必要的访问。

2.使用2D阵列:

当所有字符串的长度已知且需要特定的内存占用时,此方法非常有用。字符串的空间将在单个块中分配

这在C和C++中都得到了支持。

CPP

// C++ program to demonstrate array of strings using
// 2D character array
#include <iostream>
int main()
{
// Initialize 2D array
char colour[4][10] = { "Blue" , "Red" , "Orange" ,
"Yellow" };
// Printing Strings stored in 2D array
for ( int i = 0; i < 4; i++)
std::cout << colour[i] << "" ;
return 0;
}


输出:

BlueRedOrangeYellow

  • 字符串的数量和大小都是固定的。同样,4可能被忽略,编译器将计算出合适的大小。但是,必须给出第二个维度(在本例中为10),以便编译器可以选择适当的内存布局。
  • 每个字符串都可以修改,但会占用第二维度给出的全部空间。每一个都将在内存中并排放置,并且不能改变大小。
  • 有时,控制内存占用是可取的,这将分配一个具有固定、规则布局的内存区域。

3.使用字符串类:

STL 一串 类可用于创建可变字符串的数组。在这种方法中,字符串的大小不是固定的,可以更改字符串。

这只是在C++中支持的,因为C没有类。

CPP

// C++ program to demonstrate array of strings using
// array of strings.
#include <iostream>
#include <string>
int main()
{
// Initialize String Array
std::string colour[4] = { "Blue" , "Red" ,
"Orange" , "Yellow" };
// Print Strings
for ( int i = 0; i < 4; i++)
std::cout << colour[i] << "" ;
}


输出:

BlueRedOrangeYellow

  • 数组的大小是固定的,但不需要。同样,这里的4可以省略,编译器将确定数组的适当大小。字符串也是可变的,允许更改。

4.使用vector类:

STL容器 矢量 可用于动态分配大小不同的数组。

这只在C++中可用,因为C没有类。请注意,这里的初始化列表语法要求支持2011 C++标准的编译器,尽管编译器很可能是这样做的,但它是需要注意的。

CPP

// C++ program to demonstrate vector of strings using
#include <iostream>
#include <vector>
#include <string>
int main()
{
// Declaring Vector of String type
// Values can be added here using initializer-list syntax
std::vector<std::string> colour { "Blue" , "Red" , "Orange" };
// Strings can be added at any time with push_back
colour.push_back( "Yellow" );
// Print Strings stored in Vector
for ( int i = 0; i < colour.size(); i++)
std::cout << colour[i] << "" ;
}


输出:

BlueRedOrangeYellow

  • 向量是动态数组,允许您随时添加和删除项。
  • 向量中可以使用任何类型或类,但给定的向量只能包含一种类型。

5.使用array类:

STL容器 大堆 可用于分配固定大小的数组。它的用法可能与向量非常相似,但大小总是固定的。

这仅在C++中支持。

C++

#include <iostream>
#include <array>
#include <string>
int main()
{
// Initialize array
std::array<std::string, 4> colour { "Blue" , "Red" , "Orange" ,
"Yellow" };
// Printing Strings stored in array
for ( int i = 0; i < 4; i++)
std::cout << colour[i] << "" ;
return 0;
}


输出

BlueRedOrangeYellow

笔记:

这些并不是生成字符串集合的唯一方法。C++提供了几个 容器 类,每个类都有各种权衡和特性,它们的存在都是为了满足项目中的需求。探索并享受乐趣!

结论: 在所有的方法中,向量似乎是创建C++中字符串数组的最好方法。

本文由 卡提克·阿胡亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享