std::stringstream转换为std::vector<char>

参考:https://blog.csdn.net/wangqinghao/article/details/7245851

https://www.codenong.com/7241871/


1. std::stringstream转换为std::vector<char>


// std::stringstream转换为std::vector<char>

std::stringstream *dataStr = ......;
dataStr->seekg(0, std::ios::end);
std::streampos pos(dataStr->tellg());
if (pos)
{
  std::size_t len = static_cast<std::size_t>(pos);
  std::vector<char> buffer(len);
  dataStr->seekg(0, std::ios::beg);
  dataStr->read(&buffer.front(), len);  
}

//


//

2. std::vector<char>转换为std::stringstream


// std::vector<char>转换为std::stringstream

std::stringstream *dataStr = ......;
std::vector<char> buffer{'a','b','c','d','d'};
dataStr->rdbuf()->pubsetbuf(&buffer[0], buffer.size()); // 只做映射,没有复制
dataStr->write(&buffer[0], buffer.size()); // 进行数据的复制

//


//

posted @ 2022-12-05 16:26  ParamousGIS  阅读(88)  评论(0编辑  收藏  举报