[英]std::vector<char> to std::string
I am trying to convert a std::vector<char>
into a string using:我正在尝试使用以下方法将
std::vector<char>
转换为字符串:
// Data has been received, update the buffer...
buffer = readBuffer.data();
buffer[bytesRead-1] = '\0';
The issue I am having is that when I debug readBuffer I get "<A HREF="https://www.google.com/">here</A>."
我遇到的问题是,当我调试 readBuffer 时,我得到
"<A HREF="https://www.google.com/">here</A>."
but on reviewing buffer I get '<A HREF="https://ww ²²²²▌▌╫$Ö►¬∟☺.google.com/">here</A>.'
但在查看缓冲区时,我得到
'<A HREF="https://ww ²²²²▌▌╫$Ö►¬∟☺.google.com/">here</A>.'
Is there something really obvious I missing?我真的错过了什么吗?
Writing a null character at the end of a character vector will not magically create an std::string
.在字符向量的末尾写一个空字符不会神奇地创建一个
std::string
。 To create an actual std::string
, use something like:要创建实际的
std::string
,请使用以下内容:
std::string s = std::string(readBuffer.begin(), readBuffer.end());
You don't need to explicitly append the null character to the string, the std::string
constructor will do it for you.您不需要将空字符显式附加到字符串,
std::string
构造函数会为您完成。
你可以像这样从向量构造一个字符串:
std::string str(readBuffer.begin(), readBuffer.end());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.