copy an istringstream

D

dover

/*Copy the line a token at a time into the output*/
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(oss, " "));

What's the meaning of this statement? Thanks!
 
S

Sharad Kala

dover said:
/*Copy the line a token at a time into the output*/
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(oss, " "));

What's the meaning of this statement? Thanks!

Again as your previous post the comment gives you the answer. copy algorithm
takes three parameters - the first two are beginning and one past the end
iterators of the source range and the third parameter is the beginning
iterator of the destination range.
So your source range is a istringstream (strings can behave as external
devices to streams.) Your destination is an ostringstream with space as the
delimiter.
So if input stream contains "This is a test" then destination string will
also contain "This is a test".

-Sharad
 
R

Rolf Magnus

dover said:
/*Copy the line a token at a time into the output*/
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(oss, " "));

What's the meaning of this statement? Thanks!

It copies the line a token at a time into the output.
 
R

Russell Hanneken

dover said:
/*Copy the line a token at a time into the output*/
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(oss, " "));

What's the meaning of this statement? Thanks!

Others have answered this question. I'd like to pose a bonus question
to the group: what's the meaning of this statement?

vector<string> vec( istream_iterator<string>(iss),
istream_iterator<string>() );
 
S

Sharad Kala

Russell Hanneken said:
Others have answered this question. I'd like to pose a bonus question
to the group: what's the meaning of this statement?

vector<string> vec( istream_iterator<string>(iss),
istream_iterator<string>() );

C++'s most vexing parse! It's a function declaration and not that of a
vector. To get correct results put another set of parenthesis -
vector<string> vec( (istream_iterator<string>(iss)),
istream_iterator<string>() );

Seems you have read Effective STL ;-)

-Sharad
 
S

Sharad Kala

Russell Hanneken said:
Others have answered this question. I'd like to pose a bonus question
to the group: what's the meaning of this statement?

vector<string> vec( istream_iterator<string>(iss),
istream_iterator<string>() );

C++'s most vexing parse! It's a function declaration and not that of a
vector. To get correct results put another set of parenthesis -
vector<string> vec( (istream_iterator<string>(iss)),
istream_iterator<string>() );

Seems you have read Effective STL ;-)

-Sharad
 
R

Russell Hanneken

Sharad said:
C++'s most vexing parse! It's a function declaration and not that of a
vector. To get correct results put another set of parenthesis -
vector<string> vec( (istream_iterator<string>(iss)),
istream_iterator<string>() );

Seems you have read Effective STL ;-)

Aw, you got it right. That's no fun. :^)

I haven't actually read _Effective STL_, but I did read "Guru of the
Week" #75, which covers the issue:

http://www.gotw.ca/gotw/075.htm

If anyone's confused about why the statement above declares a function
rather than a vector, I recommend the article.
 
A

Alex Vinokur

Sharad Kala said:
C++'s most vexing parse! It's a function declaration and not that of a vector.

Is it really a function _declaration_? : iss is not type, but variable (istringstream iss).
To get correct results put another set of parenthesis -
vector<string> vec( (istream_iterator<string>(iss)),
istream_iterator<string>() );
[snip]
 
S

Sharad Kala

Is it really a function _declaration_? : iss is not type, but variable
(istringstream iss).

Yes, it is.

void foo( int d);
void foo (int (d)); // same as above; parens around d are ignored

With this knowledge, in vector<string> vec( istream_iterator<string>(iss),
istream_iterator<string>() );
the first parameter is of type istream_iterator<string>. The parentheses
around iss are superfluous and are ignored. I hope you have got the second
parameter right by yourself :)

-Sharad
 
A

Alex Vinokur

Sharad Kala said:
void foo( int d);
void foo (int (d)); // same as above; parens around d are ignored

d is of int type
With this knowledge, in vector<string> vec( istream_iterator<string>(iss),
istream_iterator<string>() );
the first parameter is of type istream_iterator<string>. The parentheses
around iss are superfluous and are ignored.
[snip]

But iss is not of istream_iterator<string> type; iss is of istringstream type.
 
S

Sharad Kala

Alex Vinokur said:
But iss is not of istream_iterator<string> type; iss is of istringstream
type.

So? You think this won't compile?

float f;
void foo (int (f)); // ==> void foo (int f);

-Sharad
 
T

tom_usenet

Sharad Kala said:
void foo( int d);
void foo (int (d)); // same as above; parens around d are ignored

d is of int type
With this knowledge, in vector<string> vec( istream_iterator<string>(iss),
istream_iterator<string>() );
the first parameter is of type istream_iterator<string>. The parentheses
around iss are superfluous and are ignored.
[snip]

But iss is not of istream_iterator<string> type; iss is of istringstream type.

Function declaration scope iss hides the prior one. This is perfectly
legal:

double d;
int f(int(d)); //function declaration, parameter named d.

Tom
 
A

Alex Vinokur

Sharad Kala said:
type.

So? You think this won't compile?

float f;
void foo (int (f)); // ==> void foo (int f);

-Sharad

OK

Now is some problem with second parameter in foo2().

------ foo.cpp ------
void foo1 (int (i), int) {}
void foo2 (int (i), int()) {}
int main()
{
foo1 (10, 20); // Line#5
foo2 (30, 40); // Line#6
return 0;
}
---------------------

------ Compilation ------
$ g++ foo.cpp
foo.cpp: In function `int main()':
foo.cpp:6: error: invalid conversion from `int' to `int (*)()'
 
S

Sharad Kala

Alex Vinokur said:
OK

Now is some problem with second parameter in foo2().

------ foo.cpp ------
void foo1 (int (i), int) {}

void foo2 (int (i), int()) {}

This declares a function, foo2, whose return type is void. It takes two
parameters -
- The first parameter is names, its type is an int
- The second parameter has NO NAME. It's type is pointer to function taking
nothing and returning int as parameter.

Probably what you are missing is that parentheses around a parameter name
are ignored (as is in the case in our first parameter), but parentheses
standing by themselves (as is in our second parameter), indicate the
existence of a parameter list. They indicate the presence of a parameter
that is
itself a pointer to a function.
int main()
{
foo1 (10, 20); // Line#5
foo2 (30, 40); // Line#6
return 0;
}
---------------------

------ Compilation ------
$ g++ foo.cpp
foo.cpp: In function `int main()':
foo.cpp:6: error: invalid conversion from `int' to `int (*)()'

The compiler error message should also give you the answer!

Hope that clears it.

-Sharad
 
A

Alex Vinokur

Sharad Kala said:
This declares a function, foo2, whose return type is void. It takes two
parameters -
- The first parameter is names, its type is an int
- The second parameter has NO NAME. It's type is pointer to function taking
nothing and returning int as parameter.

Probably what you are missing is that parentheses around a parameter name
are ignored (as is in the case in our first parameter), but parentheses
standing by themselves (as is in our second parameter), indicate the
existence of a parameter list. They indicate the presence of a parameter
that is
itself a pointer to a function.


The compiler error message should also give you the answer!

Hope that clears it.

-Sharad

Thanks.

Unfortunately C++ doesn't require void in function declaration.
It seems that void foo2 (int (i), int(void)) is more intuitive declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top