gpt4 book ai didi

c++ - 如何将 std::stringbuf 转换为 char 数组?

转载 作者:行者123 更新时间:2023-11-27 23:05:26 28 4
gpt4 key购买 nike

我在这里要做的是将一个 stringbuf 对象转换为一个 char 数组。

我这样做是为了将 char 数组发送到不理解类型 std::stringbufC 接口(interface)。

这是我的代码的一部分来说明问题:

std::stringbuf buffer;
char * data;

//here i fill my buffer with an object
buffer >> Myobject;
//here is the function I want to create but I don't know if it's possible
data = convertToCharArray(buffer);
//here I send my buffer of char to my C interface
sendToCInterface(data);

最佳答案

如果您没有严格的零拷贝/高性能要求,那么:

std::string tmp = buffer.str();

// call C-interface, it is expected to not save the pointer
sendToCharInterface(tmp.data(), tmp.size());

// call C-interface giving it unique dynamically allocated copy, note strdup(...)
sendToCharInterface(strndup(tmp.data(), tmp.size()), tmp.size());

如果你确实需要它很快(但仍然有 stringbuf 在路上)那么你可以看看 stringbuf::pubsetbuf() 的方向.

关于c++ - 如何将 std::stringbuf 转换为 char 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23867728/

28 4 0
文章推荐: javascript - 引用错误: Can't find variable: functionName
文章推荐: javascript - 赋值的同时赋值
文章推荐: html - 如何仅使用 CSS 设置