Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 27th, 2006, 04:45 AM
ma740988
Guest
 
Posts: n/a
Default minor confusion - std::stringstream and operator (>>/<<)

Consider:

#include <iostream>
#include <sstream>
#include <string>

int main ( ) {
{
double pi = 3.141592653589793238;
std::stringstream a;
a << pi;
double pi2;
a >> pi2;
std::cout << std::boolalpha << ( pi == pi2 ) << '\n';
}
{
std::string str = "Hello world!";
std::stringstream a;
a << str;
std::string str2;
a >> str2;
std::cout << std::boolalpha << ( str == str2 ) << '\n';
}
}


prints:

false
false

Why is that? I relied on operator << and >> to do essentially do the
right thing.

  #2  
Old March 27th, 2006, 05:15 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: minor confusion - std::stringstream and operator (>>/<<)

* ma740988:[color=blue]
> Consider:
>
> #include <iostream>
> #include <sstream>
> #include <string>
>
> int main ( ) {
> {
> double pi = 3.141592653589793238;
> std::stringstream a;
> a << pi;
> double pi2;
> a >> pi2;
> std::cout << std::boolalpha << ( pi == pi2 ) << '\n';
> }
> {
> std::string str = "Hello world!";
> std::stringstream a;
> a << str;
> std::string str2;
> a >> str2;
> std::cout << std::boolalpha << ( str == str2 ) << '\n';
> }
> }
>
>
> prints:
>
> false
> false
>
> Why is that? I relied on operator << and >> to do essentially do the
> right thing.[/color]

In the PI case it's because you're comparing floating point values
(which are approximations) for equality. Converting a floating point
value to text and then back generally does not yield exactly the same
value. Some libraries for other languages do, however, have "roundtrip"
functionality for such conversions (e.g. .NET has).

In the HULLO WORLD case it's because operator>> reads one token, the
word "Hello", from the stream, which is not "Hello world!".

Instead of operator>>, use std::getline to read one line.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old March 27th, 2006, 06:25 AM
Sumit Rajan
Guest
 
Posts: n/a
Default Re: minor confusion - std::stringstream and operator (>>/<<)

ma740988 wrote:
[color=blue]
> Consider:
>
> #include <iostream>
> #include <sstream>
> #include <string>
>
> int main ( ) {
> {
> double pi = 3.141592653589793238;
> std::stringstream a;
> a << pi;
> double pi2;
> a >> pi2;
> std::cout << std::boolalpha << ( pi == pi2 ) << '\n';
> }[/color]

See http://www.parashift.com/c++-faq-lit...html#faq-29.17
[color=blue]
> {
> std::string str = "Hello world!";
> std::stringstream a;
> a << str;
> std::string str2;
> a >> str2;
> std::cout << std::boolalpha << ( str == str2 ) << '\n';
> }
> }[/color]

str is "Hello world!" and str2 is just "Hello" since the call to >>
gets the first token only.

Regards,
Sumit.

  #4  
Old March 27th, 2006, 08:35 AM
BobR
Guest
 
Posts: n/a
Default Re: minor confusion - std::stringstream and operator (>>/<<)


ma740988 wrote in message
<1143434136.710520.295170@j33g2000cwa.googlegroups .com>...[color=blue]
>Consider:
>
>#include <iostream>
>#include <sstream>
>#include <string>
>
>int main ( ) {
> {
> double pi = 3.141592653589793238;
> std::stringstream a;
> a << pi;
> double pi2;
> a >> pi2;
> std::cout << std::boolalpha << ( pi == pi2 ) << '\n';
> }
> {
> std::string str = "Hello world!";
> std::stringstream a;
> a << str;
> std::string str2;
> a >> str2;
> std::cout << std::boolalpha << ( str == str2 ) << '\n';
> }
>}
>
>
>prints:
>false
>false
>
>Why is that? I relied on operator << and >> to do essentially do the
>right thing.[/color]

int main(){
{
double pi = 3.141592653589793238;
std::stringstream a;
int OutPre = a.precision();
a.setf(std::ios::fixed);
a.precision(20);
a << pi;
double pi2;
a >> pi2;
std::cout <<pi<<" "<<pi2<<'\n';
int OutPreC = std::cout.precision();
std::cout.setf(std::ios::fixed);
std::cout.precision(20);
std::cout <<pi<<" "<<pi2<<'\n';
std::cout << std::boolalpha << ( pi == pi2 ) << '\n';
a.precision(OutPre);
std::cout.precision(OutPreC);
}
{
std::string str = "Hello world!";
std::stringstream a;
a << str;
std::string str2;
a >> str2;
cout <<str<<" "<<str2<<'\n';
cout << std::boolalpha << ( str == str2 ) << '\n';
}
return 0;
}

// 3.14159 3.14159
// 3.14159265358979310 3.14159265358979310
// true
// Hello world! Hello
// false

--
Bob R
POVrookie


  #5  
Old March 27th, 2006, 01:35 PM
ma740988
Guest
 
Posts: n/a
Default Re: minor confusion - std::stringstream and operator (>>/<<)


BobR wrote:
[color=blue]
> int main(){
> {
> double pi = 3.141592653589793238;
> std::stringstream a;
> int OutPre = a.precision();
> a.setf(std::ios::fixed);
> a.precision(20);
> a << pi;
> double pi2;
> a >> pi2;
> std::cout <<pi<<" "<<pi2<<'\n';
> int OutPreC = std::cout.precision();
> std::cout.setf(std::ios::fixed);
> std::cout.precision(20);
> std::cout <<pi<<" "<<pi2<<'\n';
> std::cout << std::boolalpha << ( pi == pi2 ) << '\n';
> a.precision(OutPre);
> std::cout.precision(OutPreC);
> }[/color]

Bob, you're use of the precision here is something I need to get my
head around. Nice mix of stringstreams with iostream.

Anyway, thanks for the snippet
[color=blue]
> Bob R
> POVrookie[/color]

  #6  
Old March 27th, 2006, 09:15 PM
BobR
Guest
 
Posts: n/a
Default Re: minor confusion - std::stringstream and operator (>>/<<)


ma740988 wrote in message ...[color=blue]
>
>Bob, you're use of the precision here is something I need to get my
>head around. Nice mix of stringstreams with iostream.
>
>Anyway, thanks for the snippet[/color]

Your welcome. However, be aware that it is just a 'workaround', not a real
solution. It works on window$ and GNU/Linux running on the same i86 machine
(using GCC, MinGW). It may not work on other types of CPU/compiler
implementation/non-IEEE formated numbers. You should never use the equality
operator ('==') for type 'double'. Instead, compare the double to a range
unless you are *positive* the double(s) can be represented in binary *on all
machines* likely to run your code. Rounding/truncation can be a bitch! <G>

As for the stream manipulators, experiment to get used to using them. They
were confusing to me in the early days of C++, but, now it's like scratching
an itch, I don't even think about it. <G>
What you see on the CRT(screen) may be different than what is actually in the
computers memory.

--
Bob R
POVrookie


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles