What is the C++ version of sscanf?

Started by
12 comments, last by cache_hit 14 years, 3 months ago
Quote:Original post by snake5
istringstream makes useless copies of the string, allocates memory often when it's not necessary.

I really doubt that a half-decent implementation would do that.
Quote:And it is not so simple to use it.
I'd suggest using C string functions here because they also help to develop bug seeking skills. ^_^
int num1, num2;double decimal_number;std::string a_string;    //parse a string containing two ints, a floating-point number, and a stringa_stringstream >> num1 >> num2 >> decimal_number >> a_string;
You just have to keep in mind how it parses whitespace. Save your bug-seeking skills for where they're needed.

Actually, I usually use plain stringstream, which does input and output. It makes it a little easier. This page is a good reference. For stuff more complicated than that, you'll want regexes or a parsing framework. Boost.Regex and Boost.Spirit can be pretty nice. Or, depending on what you're using it for, an existing data format with pre-built parsers might be more convenient, like XML or JSON.
Advertisement
Quote:Original post by theOcelot
Quote:Original post by snake5
istringstream makes useless copies of the string, allocates memory often when it's not necessary.

I really doubt that a half-decent implementation would do that.


It's not always possible to prevent. Consider, for example, the following:

string s1, s2, s3, s4;

s1 = "Hello,";
s2 = " Worl";
s3 = "d!";
s4 = s1 + s2 + s3;

How many times is memory allocated in statement 4? How many times is memory deallocated in statement 4?




The correct answer is that memory is allocated 3 times and deleted 2 times.

Allocation 1: A temporary T1 to hold the result of s2 + s3
Allocation 2: A temporary T2 to hold the result of s1 + s2 + s3
Allocation 3: A copy made of T2 to store in s4
Deallocation 1: T1
Dallocation 2: T2

But this is hardly optimal. You could do better by eliminating Allocation 3 and Deallocation 2, after all s4 is an exact copy of the result of the expression, why make another allocation to hold an exact copy of this temporary when the internals of the temporary itself could be used?

C++0x rvalue references and move constructors solve this. It's not possible under C++ currently.

This is, of course, just one example. There are numerous cases in STL, Boost, and other library code where temporaries / copies are not preventable but could be eliminated under C++0x. The return value optimization helps sometimes, but not always.
Quote:Original post by cache_hit
Quote:Original post by theOcelot
Quote:Original post by snake5
istringstream makes useless copies of the string, allocates memory often when it's not necessary.

I really doubt that a half-decent implementation would do that.


It's not always possible to prevent. Consider, for example, the following:

He was talking about stringstream, not string concatenation operator.
i'm aware, i used that because its an easy to illustrate example of a very widespread problem, and i wouldnt be surprised if its (at least partly) the problem with stringstream

This topic is closed to new replies.

Advertisement