在地图内初始化字符串流?

发布于 2024-09-10 02:24:25 字数 520 浏览 4 评论 0原文

如何在地图内后初始化字符串流?

是否有可能或者我必须创建一个 stringstream* ?

std::map<std::string, std::stringstream> mapTopics;

if(mapTopics.end() == mapTopics.find(Topic))
{
    mapTopics[Topic] = std::stringstream(""); // Post Initialize <---
}

std::map<std::string, std::stringstream>::iterator  mapTopicsIter = mapTopics.find(Topic);
mapTopicsIter->second << "    <say speaker=\"" << sSpeaker << "\">" << label << "</say>" << std::endl;

How can I post-initialize an stringstream inside a map?

Is it even possible or do I have to create a stringstream*?

std::map<std::string, std::stringstream> mapTopics;

if(mapTopics.end() == mapTopics.find(Topic))
{
    mapTopics[Topic] = std::stringstream(""); // Post Initialize <---
}

std::map<std::string, std::stringstream>::iterator  mapTopicsIter = mapTopics.find(Topic);
mapTopicsIter->second << "    <say speaker=\"" << sSpeaker << "\">" << label << "</say>" << std::endl;

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

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

发布评论

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

评论(2

原谅过去的我 2024-09-17 02:24:25

如何在地图内后初始化字符串流?

你不能。 STL容器要求其数据元素可复制,而流则不可复制。

为什么要在地图中包含流?你不能存储字符串吗?

如果你真的很绝望,你将不得不存储指向(很可能是动态分配的)字符串流的指针:

std::map<std::string, std::shared_ptr<std::stringstream> > stream_map;

这样做的优点是,如果你存储指向流基类的指针,你以后还可以将其他流添加到映射中。

How can I post-initialize an stringstream inside a map?

You cannot. STL containers require their data elements to be copyable, and streams are not copyable.

Why do you want to have streams in a map? Can't you store the strings?

If you are really desperate, you will have to store pointers to (most likely dynamically allocated) string streams:

std::map<std::string, std::shared_ptr<std::stringstream> > stream_map;

This has the advantage that, would you store pointers to a stream base class, you could later also add other streams to the map.

匿名的好友 2024-09-17 02:24:25

不确定这是否是你的意思,但是怎么样:

std::stringstream ss;
ss << "blablablabla";
ss.str("") /*Initialize*/

Not sure If its what you mean but how about :

std::stringstream ss;
ss << "blablablabla";
ss.str("") /*Initialize*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文