我可以在 C 中使用什么而不是"string" C++
What can I use in C instead of "string" from C++
我已经开始在C中进行纸牌游戏,但是我找不到任何东西,而不是string
,显然没有在C中使用。这里有一些代码,其中这成为一个障碍:
struct cards {
string index;
string colour;
};
void load_cards(cards deck[MAX_C])
{
int i = 0;
for (int j = 0; j < 4; j++) {
deck[i].index = '2';
deck[i += 1].index = "3";
deck[i += 1].index = "4";
deck[i += 1].index = "5";
deck[i += 1].index = "6";
deck[i += 1].index = "7";
deck[i += 1].index = "8";
deck[i += 1].index = "9";
deck[i += 1].index = "10";
deck[i += 1].index = "W";
deck[i += 1].index = "D";
deck[i += 1].index = "K";
deck[i += 1].index = "A";
}
for (int j = 0; j < 13; j++) {
deck[j].colour = "T";
}
for (int j = 13; j < 26; j++) {
deck[j].colour = "k";
}
for (int j = 26; j < 39; j++) {
deck[j].colour = "K";
}
for (int j = 39; j < 52; j++) {
deck[j].colour = "P";
}
for (int j = 52; j < 56; j++) {
deck[j].colour = "JR";
}
}
string
实际上是C 的类,正如其他人指出的那样,它是围绕char数组/指针的包装器(可用于存储chars
的序列)。
在C中,您必须手动使用字符数组。您可以通过两种方式(主要是)进行:
- 创建一个静态字符数组(大小足够)。
- 使用
malloc
动态创建字符数组。但是,在不再需要数组时,您也必须手动释放它。
当然,由于您使用的是C,您必须明确/手动,照顾好数组溢出条件,重叠,终止等等。
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串