You may want to declare your istringstream outside the loop and use the str() function of istringstream in the loop. This would possibly be a bit more efficient since the istringstream object would not be constructed and destructed at each iteration of the loop.
here's a small example on the current project i'm working on.
1 2 3 4 5 6 7 8
for(x = 0;x<number;x++)
{
//Reinitialized every iteration
ostringstream child_file;
string in_text,out_text,child_name;
int endp,startp,last_char,start_char,total_char;
startp = 4096*x;
}
versus
1 2 3 4 5 6 7 8 9 10 11
int main()
{
istringstream stuff;
fstream otherstuff;
otherstuff.open("I like the word stuff")
while(otherstuff.good() == true) //can be rewritten as just while(otherstuff.good())
{
char a = otherstuff.get();
stuff << a;
}
}
The difference is really a public member versus private member of a function (though they likely aren't called that). The second example is public, stuff can be called anywhere in int main(), where as the first example, and your example, child_file is private as it can only be called in that particular loop and is reinitialized on every iteration which clears it's current information. Though it is useful to use private variables for certain things, you have to be careful where it's initialized as that can cause problems if you don't want it to be cleared.
With stringstreams it's also important to note that they are cumulative in practice, as in
would return ab. The major difference is if you want it to return every iteration of information, declare it outside of the loop, whereas if you want it to hold only one iteration of information, declare it inside the loop.
Although, I conclude it's primarily a matter of elegance/cleariness, not of performance.
I believe that the declaration within a loop looks ugly but does not produce extra code.
depending on how many other things are going on and how large your program is, yes there could be a toll. However I doubt that it would be so much of a toll as to be a deal breaker. But if you just wanted to clear information every iteration, it would be just as easy imo to have one of your last lines be stringstream name; name.str().empty(), again it mostly just depends on what you're doing and why you're doing it.
EDIT: i do not know how name.str().empty() would effect the hexidecimal interpretation of the stringstream though, at which point reinitializing it may be better.
I don't think name.str().empty(); would do anything to name at all...
I guess you could call name.flush(); since I think that calls the internal sync (but you might have to call clear() first in case a flag is set) or I've seen people do name.str( "" );