istringstream in a loop

I'm not sure if I'm doing a good thing. I'd like some advise.

I need to read lines from a text file where the delimiter of fields is semicolon. I'm doing this:

while (getline(inpfile,row)) {

istringstream streamline(row);

getline(streamline, field1, ';' );
getline(streamline, field2, ';' );
getline(streamline, field3, ';' );
...
...
}

Straight Question: does it make sense to have istringstream INSIDE a loop?

Thanks
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.
LLoyd, that's EXACTLY what I needed to know! Thanks a bunch.

I would appreciate if you also write a small example as I'm really unfamiliar with this.

Regards
closed account (4Gb4jE8b)
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

1
2
3
4
stringstream stuff;
stuff << "a";
stuff << "b";
cout << stuff;


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.

I hope this helped and made sense
Last edited on
Thanks a lot. It makes perfect sense.

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.

Regards
it doesn't produce extra code, true, but wouldn't there be a cost to recreating and initializing every iteration?
closed account (4Gb4jE8b)
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.
Last edited on
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( "" );

-- hmm, maybe the str() was for stringstream...
Last edited on
closed account (4Gb4jE8b)
or you could just reinitialize it =P

oh but i guarantee that name.str("") does NOT clear the hexadecimal status of the stringstream. I've tried it before, it didn't work.

It's all multiple ways of doing the exact same thing anyway, so in this case, i think it's fair to say ehh w/e
Last edited on
Consider:
1
2
3
4
5
6
7
8
9
10
11
std::istringstream ss("hello there!");
std::string str;
int i = 5;
while (i-- > 0) {
    ss.clear();
    ss.str("hi here!");
    ss >> str;
    std::cout << str << " ";
    ss >> str;
    std::cout << str << "\n";
}
closed account (4Gb4jE8b)
hi there... times five?
Topic archived. No new replies allowed.