如何在C ++中声明字符串数组?

时间:2021-09-18 20:56:35

In C++ how can I declare an array of strings? I tried to declare it as an array of char but that was not correct.

在C ++中如何声明字符串数组?我试图将它声明为char数组,但这不正确。

3 个解决方案

#1


16  

#include <string>

std::string my_strings[100];

That is C++, using the STL. In C, you would do it like this:

那就是C ++,使用STL。在C中,你会这样做:

char * my_strings[100];

This reads as "my strings is an array of 100 pointer to char", and the latter is how strings are represented in C.

这读作“我的字符串是100个指向char的指针的数组”,后者是字符串在C中的表示方式。

#2


13  

I would rather recommend using a vector of strings in almost every case:

我宁愿建议在几乎所有情况下都使用字符串向量:

#include <string>
#include <vector>
std::vector<std::string> strings;

#3


0  

Conventional single string:

常规单弦:

char foo[100] // foo is a 100 character string

What you need is probably:

你需要的可能是:

char foobar[100][100] // foobar is a 100 member array of 100 character strings

#1


16  

#include <string>

std::string my_strings[100];

That is C++, using the STL. In C, you would do it like this:

那就是C ++,使用STL。在C中,你会这样做:

char * my_strings[100];

This reads as "my strings is an array of 100 pointer to char", and the latter is how strings are represented in C.

这读作“我的字符串是100个指向char的指针的数组”,后者是字符串在C中的表示方式。

#2


13  

I would rather recommend using a vector of strings in almost every case:

我宁愿建议在几乎所有情况下都使用字符串向量:

#include <string>
#include <vector>
std::vector<std::string> strings;

#3


0  

Conventional single string:

常规单弦:

char foo[100] // foo is a 100 character string

What you need is probably:

你需要的可能是:

char foobar[100][100] // foobar is a 100 member array of 100 character strings