473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

string vs. ostringstream

Hello,

we often compose strings via a ostringstream and then create a string
from it. What is the rationale of not being able to use string in
place of a ostringstream, so I could write

string str;
str << ... << ...;
SomeAPI( str.c_str() );

Strangely enough, there is an string::operator+= that lets me
concatenate strings. Isn't ostringstream::operator<< also implemented
such that it first finds out the potential length of its argument,
then makes sure its buffer is long enough, and then lets the argument
fill the buffer with its representation? Wouldn't the same work just
as efficiently for string?

Thanks for insights,

Arno
Jan 16 '08 #1
1 8065
On Jan 16, 6:48 pm, scho...@gmail.com wrote:
we often compose strings via a ostringstream and then create a string
from it. What is the rationale of not being able to use string in
place of a ostringstream, so I could write
string str;
str << ... << ...;
SomeAPI( str.c_str() );
Good design. Formatting is a different concern than just
holding text. (Not that std::string is particularly well
designed for managing text either. But then, I'm not sure, even
today, that we know enough to specify a definitive text type.)
Strangely enough, there is an string::operator+= that lets me
concatenate strings. Isn't ostringstream::operator<< also
implemented such that it first finds out the potential length
of its argument, then makes sure its buffer is long enough,
and then lets the argument fill the buffer with its
representation?
Not necessarily. Certainly not, in fact---there is no
ostringstream::operator<<. Just an ostream::operator<<.

You are dealing with two different abstractions. The
ostream::operator<< (and the free operator<< functions which
take an ostream& as their first argument) are concerned with
formatting textual output (and uniquely with formatting textual
output). They use the strategy pattern to handle the data
sink---what to do with the characters they generate. The
various streambuf classes handle data sinking and sourcing.

std::string is more or less just another container, with a few
extra operations, but with some important restrictions (e.g. it
can only "contain" POD types). But even if std::string were
"text", it would be important to separate the concerns of
formatting other types from it.

Note that "formatting", in general, is an awkward problem.
Obviously, you don't want to attach it to the data sink---why
should a file (or a string) know about your types. But
attaching it to the type being formatted isn't right either:
why should a simple type like e.g. complex be loaded down with
knowledge about all possible formats (normal text, XML, XDR,
BER...). In the end, iostream got it right: formatting is a
free function with a "standard" invocation sequence which
supports chaining.
Wouldn't the same work just as efficiently for string?
At the processing level, it could possibly be made to work even
more effectively. For the user, however, it would be a
disaster.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 17 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: __PPS__ | last post by:
Hello, I was thinking about one solution, but no luck. suppose I have an std::string object. I want to be able to use this string like std::ostringstream. something like: string str;...
3
by: Someonekicked | last post by:
in the program im writing, i was going to output this using cout : cout << left << setw(10) << newAccount.getNumber() << setw(18) << newAccount.getName() << setw(18) << newAccount.getFamName() <<...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
3
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s...
5
by: probstm | last post by:
I am using a message handler class that implements amongst others: static void message ( std::string aHeader, std::string aMessage, int aTimeout = 0, MessageType aFlag = MSG_Information ); ...
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
2
by: Adam Nielsen | last post by:
Hi everyone, Following advice previously given in this group, I've created a function that I'm using to "inline" the creation of a string. Regardless of what you think of my method, I'm...
11
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
9
by: jl_post | last post by:
Hi, A few months back I remember reading through C++ newsgroups trying to find a way to quickly convert a number to a C++ std::string. I often see code like: // Create a string that holds a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.