2015-09-16 51 views
0

文本我有一个获得从二元矢量

std::vector<unsigned char> data; 

其中包含二进制读文件。

如果我把它写入

std::ofstream outputFile("file", std::ios_base::binary); 

我会看到它常规的文本。然后我可以将它读入std :: string中,这会占据文本。 是否有可能将矢量直接复制到具有相同结果的字符串?

+0

是,'的std :: string S(data.begin(),data.end());'或者,如果你已经有了一个字符串:'s.assign(data.begin(),data.end());' – Galik

回答

2

使用std::string的构造的第六形式:

// Example program 
#include <iostream> 
#include <string> 
#include <vector> 

int main() 
{ 
    std::vector<unsigned char> data{'a', 'b', 'c'}; 
    std::string str(data.begin(), data.end()); 
    std::cout << str << std::endl; 
} 

Live demo here