I am from C#, Java background trying to learn C++. The following is a snippet I found for reading values for a structure variable. Could not quite figure out what is being done or whether this is the standard way of reading a string structure member. Isn't 开发者_开发知识库there a more cleaner way to do this.
string mystr;
struct structdefn
{
string member;
...
} mystruct[10];
...
getline (cin,mystr);
stringstream(mystr) >> mystruct[n].member;
That code uses a temporary stringstream
fed with a line read from stdin to read the data into the struct member. Although there are some reasons for which you may be doing that (e.g. to check that the >>
operator consumes the whole line), generally you could more simply do
cin >> mystruct[n].member;
As for the explanation of your code:
getline
reads a whole line fromcin
(the standard input stream) and puts it into thestring
mystr;- a temporary object of type
stringstream
is then created;stringstream
s are a particular type of streams which aren't fed from a file/socket/whatever, but from astring
. In this case, the string ismystr
, the line just read from the standard input; - on that temporary object is called the operator
>>
, which, in this context, is called the extraction operator1; it's work is to extract from the stream and convert the data that will be put in the variable on its right, in this casemystruct[n].member
.
The simpler code I posted just uses the operator >>
directly from the standard input stream (cin
), thus avoiding the intermediate passage of the stringstream
.
1. Although with integers it's the binary right shift operator (which it's its original meaning).
Assuming that mystr
and mystruct[n].member
are both std::string
instances, I'm not sure why the stringstream
is being used - you should be able to simply read directly into the member
string.
#include <iostream>
#include <string>
// Assuming that mystruct[n].member is a std::string
std::getline ( std::cin, mystruct[n].member );
It would be more helpful to have the declaration of the mystruct
structure.
EDIT:
You can make use of the stringstream
if the data you are reading and storing into the structure member is not the entire line of text. The extraction operator >>
will extract a "word" at a time from the string, where whitespace delimits the "words" in the line of text. Example:
// Where the line of text is "123 abc pdq" ...
std::string s;
std::getline ( std::cin, s ); // s now contains: 123 abc pdq
std::stringstream ss ( s );
ss >> mystruct[n].member; // member will contain: 123
There are ways to change the behavior of the stream regarding how the whitespace is handled, but this is the most commonly used way of extracting parts of a string of text into other strings.
The std::stringstream class is useful for complex string formatting (think printf, but type-safe), and converting between strings and other types.
For example, where you would use Integer.parseInt in Java, you would use std::stringstream in C++.
int i;
std::stringstream("123") >> i;
If you don't need to do conversion, and you don't need to string formatting, then you probably don't need a stringstream.
精彩评论