<char> 零到字符串的向量
vector<char> with zeros to string
我有一个
vector<char> sectionData;
我想看看它的内容。我尝试以下
string sectionDataStr = string(sectionData.begin(),sectionData.end());
但我只得到字符串中显示的sectionData的一部分,因为sectionData包含零。如果在任何情况下我想将整个数据读取到字符串中,我该怎么办?
我不能使用std::cout感谢
您的问题不存在。这里有一个简单的演示:
#include <string>
#include <iostream>
int main()
{
std::cout << std::string { 'a', ' ', 'b', ' ', 'c' } << std::endl;
}
现在检查输出:
$ ./a.out | hexdump -C
00000000 61 00 62 00 63 0a
## a b c n
正如你所看到的,一切都在那里。
或者(根据您的编辑):
#include <cstdio>
std::fwrite(sectionDataStr.data(), sectionDataStr.size(), stdout);
问题不在于字符串,而在于输出。根据您使用的内容,输出例程可能会在字符串的第一个0处停止。如果你不想这样,那么你可能需要使用类似的东西来转换缓冲区以进行输出
std::replace( sectionData.begin(), sectionData.end(), ' ', ' ' )
相关文章:
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 在将字符串放入字符串向量时遇到问题?
- 如何从字符串向量构造对象并避免复制?
- 如何在目录及其子文件夹中构建文件名字符串向量?
- 当映射包含字符串向量作为值时,从值中获取键的有效方法
- 将字符串向量中的字符串放入主字符串中
- 如何在 c++ 中从字符串向量中读取
- 用字符串的向量分配字符串向量
- 如何使用 STL 算法将整数向量转换为字符串向量?
- C++ 中字符串向量的索引
- 如何根据第二列/第三列等对字符串向量进行排序?
- 如何从输入中获取字符串向量?
- 使用 C++-17,如何从字符串向量填充元组
- 对于循环增量器不能用作字符串向量的索引
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- 如何从字符串向量中选择第 n 个位置?
- 如何将csv中的数据放入c++中的字符串向量中,而绝对没有任何作用
- 反转不带反转函数的字符串向量
- 如何在字符串向量中指向const int
- 如何在字符串向量中找到某个值