如何高效复制istringstream?

发布于 2024-08-05 14:22:41 字数 133 浏览 2 评论 0原文

还是奥斯特流?

istringstream a("asd");
istringstream b = a; // This does not work.

我想 memcpy 也不起作用。

Or ostringstream?

istringstream a("asd");
istringstream b = a; // This does not work.

I guess memcpy won't work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-08-12 14:22:41
istringstream a("asd");
istringstream b(a.str());

编辑:
根据您对其他回复的评论,听起来您可能还想将 fstream 的全部内容复制到 strinstream 中。你也不想/必须一次只做一个角色(你是对的——这通常很慢)。

// create fstream to read from
std::ifstream input("whatever");

// create stringstream to read the data into
std::istringstream buffer;

// read the whole fstream into the stringstream:
buffer << input.rdbuf();
istringstream a("asd");
istringstream b(a.str());

Edit:
Based on your comment to the other reply, it sounds like you may also want to copy the entire contents of an fstream into a strinstream. You don't want/have to do that one character at a time either (and you're right -- that usually is pretty slow).

// create fstream to read from
std::ifstream input("whatever");

// create stringstream to read the data into
std::istringstream buffer;

// read the whole fstream into the stringstream:
buffer << input.rdbuf();
多像笑话 2024-08-12 14:22:41

您不能只复制流,还必须使用迭代器复制它们的缓冲区。例如:

#include <sstream>
#include <algorithm>
......
std::stringstream first, second;
.....
std::istreambuf_iterator<char> begf(first), endf;
std::ostreambuf_iterator<char> begs(second);
std::copy(begf, endf, begs);

You can't just copy streams, you have to copy their buffers using iterators. For example:

#include <sstream>
#include <algorithm>
......
std::stringstream first, second;
.....
std::istreambuf_iterator<char> begf(first), endf;
std::ostreambuf_iterator<char> begs(second);
std::copy(begf, endf, begs);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文