gpt4 book ai didi

c++ - 将 std::string 转换为 boost::asio::streambuf

转载 作者:行者123 更新时间:2023-11-30 03:30:29 34 4
gpt4 key购买 nike

如何将 std::string 转换为 boost::asio::streambuf?任何领导表示赞赏。

最初我使用 boost::asio::read_until(pNetworkConnector->getSocket(), response, "\r\n"); 初始化了 boost::asio::streambuf response 并将其转换为底层流,即

boost::asio::streambuf* responsePointer = &response;
iResponseStream.rdbuf(responsePointer);

但现在我像这样使用 curl 直接得到一个 std::string

static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

并将其用作

curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &readBuffer);

如何将 std::string 转换为 boost::asio::streambufstd::streambuf

最佳答案

下面的代码展示了如何在std::stringboost::asio::streambuf之间进行转换,参见online at ideone :

#include <iostream>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/buffer.hpp>

int main()
{
/* Convert std::string --> boost::asio::streambuf */
boost::asio::streambuf sbuf;
std::iostream os(&sbuf);
std::string message("Teststring");
os << message;

/* Convert boost::asio::streambuf --> std::string */
std::string str((std::istreambuf_iterator<char>(&sbuf)),
std::istreambuf_iterator<char>());

std::cout << str << std::endl;
}

关于c++ - 将 std::string 转换为 boost::asio::streambuf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44904295/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com