• Advertisement
Sign in to follow this  

Printing a sequence to cout. Problem with trailing output

This topic is 3929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiyas I'm printing a sequence of map keys to cout using
template<class K, class V> 
string pairToString(const pair<K, V*>& p) { return p.first; }
...
transform(location.units.begin(), location.units.end(), 
	ostream_iterator<string>(out, ", "), pairToString<string, Unit>);
and I'm getting an extra ", " at the end of the output. How do I get rid of that in a slick way?

Share this post


Link to post
Share on other sites
Advertisement
Hmm this is the closest I can get...

template<class K, class V>
struct stringAppender
{
stringAppender(std::string& s) : m_str(&s) {}

void operator()(const std::pair<K, V*>& p) const
{
static bool mFirst = true;
*m_str += (mFirst ? "" : ", ") + p.first;
mFirst = false;
}

std::string* m_str;
};
...
string s = "Available actions: ";
std::copy(unit.actions.begin(), unit.actions.end(),
boost::make_function_output_iterator(stringAppender<string, Action>(s)));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement