Hendrik
Hendrik

Reputation: 131

Post-Initialize stringstream inside map?

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;

Upvotes: 2

Views: 2306

Answers (2)

Johnny
Johnny

Reputation: 1

Not sure If its what you mean but how about :

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

Upvotes: 0

sbi
sbi

Reputation: 224149

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.

Upvotes: 4

Related Questions