vector <char> with zeros to string </char>

时间:2013-02-17 11:48:57

标签: c++ string vector

我有一个

vector<char> sectionData;

我希望看到它的内容。我尝试以下

string sectionDataStr = string(sectionData.begin(),sectionData.end());

但是我只得到字符串中提供的sectionData的一部分,因为sectionData包含零。我可以做什么,如果有的话我想将整个数据读成字符串?

我不能使用std :: cout 感谢

2 个答案:

答案 0 :(得分:2)

您的问题不存在。这是一个简单的演示:

#include <string>
#include <iostream>

int main()
{
    std::cout << std::string { 'a', '\0', 'b', '\0', 'c' } << std::endl;
}

现在检查输出:

$ ./a.out | hexdump -C

00000000  61 00 62 00 63 0a 

##         a \0  b \0  c \n

正如你所看到的,它就在那里。


或者(编辑后):

#include <cstdio>

std::fwrite(sectionDataStr.data(), sectionDataStr.size(), stdout);

答案 1 :(得分:0)

您的问题不是字符串,而是输出。根据您使用的内容,输出例程可能会停止在字符串的前0位。如果您不想这样做,那么您可能需要使用以下内容将缓冲区转换为输出:

std::replace( sectionData.begin(), sectionData.end(), '\0', ' ' )