【发布时间】:2010-06-27 08:14:37
【问题描述】:
我在 C++ 中有一个 char 数组,它看起来像 {'a','b','c',0,0,0,0}
现在我将它写入一个流,我希望它看起来像“abc”,其中包含四个空格 我主要使用std::stiring,我也有提升。 我如何在 C++ 中做到这一点
基本上我认为我正在寻找类似的东西
char hellishCString[7] = {'a','b','c',0,0,0,0}; // comes from some wired struct actually...
std::string newString(hellishCString, sizeof(hellishCString));
newString.Replace(0,' '); // not real C++
ar << newString;
【问题讨论】:
-
std::string可以包含嵌入的空字符吗?另一种选择可能是改用std::vector。 -
@dreamlax:是的,他们可以。字符串的长度是独立存储的,因此不需要空终止,空字符也不会被特殊处理。