472,992 Members | 3,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

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.

Mar 27 '06 #1
5 3497
* ma740988:
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.


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?
Mar 27 '06 #2
ma740988 wrote:
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';
}
See http://www.parashift.com/c++-faq-lit...html#faq-29.17
{
std::string str = "Hello world!";
std::stringstream a;
a << str;
std::string str2;
a >> str2;
std::cout << std::boolalpha << ( str == str2 ) << '\n';
}
}


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

Regards,
Sumit.

Mar 27 '06 #3

ma740988 wrote in message
<11**********************@j33g2000cwa.googlegroups .com>...
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.


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
Mar 27 '06 #4

BobR wrote:
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);
}
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
Bob R
POVrookie


Mar 27 '06 #5

ma740988 wrote in message ...

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


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
Mar 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Giampiero Gabbiani | last post by:
Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success... Thanks in advance Giampiero
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
5
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
1
by: magix | last post by:
I got this reply in my previous post a month ago: May I know, how can I automatically create the folder if it doesn't exist ? In previous reply, it said: -------------------------...
2
by: akitoto | last post by:
Hi there, I am using the std::stringstream to create a byte array in memory. But it is not working properly. Can anyone help me? Code: #include <vector> #include <sstream> #include...
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
7
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.