<char> 零到字符串的向量

vector<char> with zeros to string

本文关键字:字符串 向量 gt lt char      更新时间:2023-10-16

我有一个

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(), '', ' ' )