Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Truncate std__ostringstream?

17 views
Skip to first unread message

MikeCopeland

unread,
Jun 2, 2016, 8:45:29 PM6/2/16
to
Is there a way to truncate 1 or more characters from a constructed
std::ostringstream? I have situations where I build a stream
(data_value1, comma, data_value2, comma, etc.) and when complete I want
to erase the last comma before I process the constructed variable.
Currently, I convert the stream to a std::string and erase the last
character, but that's clumsy.
Can I do this while it's an ostringstream? Please advise. TIA


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Alf P. Steinbach

unread,
Jun 2, 2016, 9:56:50 PM6/2/16
to
On 03.06.2016 02:45, MikeCopeland wrote:
> Is there a way to truncate 1 or more characters from a constructed
> std::ostringstream? I have situations where I build a stream
> (data_value1, comma, data_value2, comma, etc.) and when complete I want
> to erase the last comma before I process the constructed variable.
> Currently, I convert the stream to a std::string and erase the last
> character, but that's clumsy.
> Can I do this while it's an ostringstream? Please advise. TIA

You can change the logic used to put items into the ostream, like this:

int n_written = 0;
for( auto const& item : items )
{
if( n_written > 0 ) { stream << ", "; }
stream << item;
++n_written;
}

Cheers & hth.,

- Alf

0 new messages