在C 中可能是字符串数组
is string array possible in c++?
据我所知,在C string
中本身就是char
的数组。
所以我的问题是:
是否可以在C ?
如果是,请让我知道声明/处理它的方法。
当然是:
// Requires <string> and <vector> includes
std::vector<std::string> foo = {"this", "is", "a", "string", "array"};
或
// Requires <string> and <array> includes
std::array<std::string, 3> foo = {"if", "you", "must"};
作为经验法则,始终使用使用std::vector
,除非您能想到一个很好的理由不。
在现代C 中,我们尽量不要将字符串视为字符数组。标准库中的一些高级工具提供了我们需要的间接水平。
五个字符串的数组:
std::array<std::string, 5> = {"init.", "generously", "provided", "by", "Bathsheba" };
字符串的动态阵列:
std::vector<std::string> = { "as", "much", "strings", "as", "one", "wants" };
请参阅他们的相关文档:
-
std::string
-
std::array
-
std::vector
字符串是一种类型。与任何其他类型一样,可以在C 中定义一系列字符串:
std::string myarray[] = {"Hello", "World", "Lorem"};
或:
std::vector<std::string> myarray = {"Hello", "World", "Lorem"};
或:
std::array<std::string, 3> myarray = {"Hello", "World", "Lorem"};
请确保包括<string>
和其他适当的标头。这是STD :: String类模板的更多信息。
这是您想要的:
#include <string>
// ...
std::string str_array[] = {"some", "strings", "in", "the", "array"};
但实际上您最好使用其他答案中所示的std::array
或std::vector
。
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组