whatever.clear();
It looks like the designers were not consistent, since when applied to
ostringstream the 'clear()' member function sets some flags or
something. I also tried:
myostringstream.flush();
But it is not working, either.
TIA,
-Ramon
Don't reuse a stream.
Just create a new stream object.
Cheers & hth.,
- Alf
That's what I reluctantly did - What can I say, I am a newborn
recycler. :^D
-Ramon
This works with all container classes that came from the STL or are inspired
by it.
> It looks like the designers were not consistent, since when applied to
> ostringstream the 'clear()' member function sets some flags or
> something.
For your info: The design of the STL and the design of the IOStreams took
place separately by independent designers before they were later on
incorporated into the C++ standard library. This explains the stylistic
differences between the two.
> I also tried:
>
> myostringstream.flush();
>
> But it is not working, either.
What exactly do you want? An explicit 'flush()' should empty all the buffers
by actually performing the requested output operations, giving you empty
buffers. Not that there are actually ways for you to detect that, so this
can't be what you want.
Uli
myostringstream.str("");
I KNEW that there had to be some way to empty things. There always
are. I will try it right away...
My code looks like this:
istringstream str2int(year_str);
int year;
str2int >> year;
Which is rather nice, but it does not scale well (stylistically
speaking):
istringstream str3int(another_str);
int another;
str3int >> another;
istringstream str4int(some_str);
int some;
str4int >> some;
istringstream str5int(whatever_str);
int ;
str5int >> whatever;
-Ramon
>
> I KNEW that there had to be some way to empty things. There always
> are. I will try it right away...
>
> My code looks like this:
>
> istringstream str2int(year_str);
> int year;
> str2int >> year;
>
> Which is rather nice, but it does not scale well (stylistically
> speaking):
>
> istringstream str3int(another_str);
> int another;
> str3int >> another;
>
> istringstream str4int(some_str);
> int some;
> str4int >> some;
>
> istringstream str5int(whatever_str);
> int ;
> str5int >> whatever;
Duplicating a piece of code never scales well (especially stylistically
speaking).
Once you find that you need the same piece of code in two (or three, or
more) places, it becomes time to move that piece of code to a separate
function:
int str2int(const string& src)
{
int dest;
istringstream iss(src);
iss >> dest;
return dest;
}
int year = str2int(year_str);
int another = str2int(another_str);
int some = str2int(some_str);
int whatever = str2int(whatever_str);
>
> -Ramon
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
You are absolutely right, Bart.
Thanks!
-Ramon
[ ... ]
> My code looks like this:
>
> istringstream str2int(year_str);
> int year;
> str2int >> year;
What you have is virtually identical to a boost lexical_cast:
int year = lexical_cast<int>(year_str);
Another possibility looks something like this:
template <class T>
void convert(std::string const &input, T &result) {
std::istringstream cvt(input);
cvt>>result;
}
which you'd use something like:
int year;
convert(year_str, year);
As it stands at the moment, the second provides no real advantage --
but if you want to assure that (for example) the _entire_ input
string converted to the desired result type, you can add that pretty
easily:
template <class T>
bool convert(std::string const &input, T &result) {
std::istringstream cvt(input);
cvt>>result;
char ch;
return !(cvt>>ch);
}
if (!convert(str_year, year))
// str_year didn't (entirely) convert
--
Later,
Jerry.