S
subramanian100in
I am using g++ 3.4.3
Consider the following program x.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
istream_iterator<string> isi(cin);
istream_iterator<string> eos;
vector<string> c(isi, eos);
cout << "Displaying input contents:" << endl;
cout << "--------------------------" << endl;
copy(c.begin(),
c.end(),
ostream_iterator<string>(cout, "\n"));
cout << endl;
if (isi == eos)
cout << "end of input stream reached" << endl;
else
cout << "eos not reached" << endl;
string str;
if (cin >> str)
cout << "string read from cin: " << str << endl;
else
cout << "Unable to read a string from cin" << endl;
cout << endl;
vector<string> d(isi, eos);
cout << "Displaying the input second time:" << endl;
cout << "---------------------------------" << endl;
copy(d.begin(),
d.end(),
ostream_iterator<string>(cout, "\n"));
return EXIT_SUCCESS;
}
When I run this program with the following input
1
2
3
the following is printed:
Displaying input contents:
--------------------------
1
2
3
eos not reached
Unable to read a string from cin
Displaying the input second time:
---------------------------------
1
Kindly correct me whereever I am wrong in the following:
In the above program, consider the statement:
vector<string> c(isi, eos);
This vector ctor will construct the object 'c' with three input values
namely '1', '2', '3' which were entered from the standard input. After
this ctor statement, there will NOT be any input contents left in cin.
Am I correct ?
The first 'copy' algorithm will copy the contents of 'c' onto the
standard output. Now consider the statement
if (isi == eos)
Here I thought 'isi' should be equal to 'eos' because there is no
content left in 'cin'. But on the contrary, 'eos not reached' is
printed. I am unable to understand the reason for this.
Now consider the statement
if (cin >> str)
For this, the control goes to the else part(because there is no
content left in the stanadard input stream 'cin') and prints:
Unable to read a string from cin
This is as expected.
Now consder the vector object:
vector<string> d(isi, eos);
Since there is no content left in 'cin', the vector object 'd' should
be empty I thought. However, on the contrary, the subsequent 'copy'
algorithm prints:
1
I am unable to understand how the second vector object d's ctor was
able to read 'isi' to fetch the value '1'. In other words, where does
this value '1' come from ?
Kindly explain.
Thanks
V.Subramanian
Consider the following program x.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
istream_iterator<string> isi(cin);
istream_iterator<string> eos;
vector<string> c(isi, eos);
cout << "Displaying input contents:" << endl;
cout << "--------------------------" << endl;
copy(c.begin(),
c.end(),
ostream_iterator<string>(cout, "\n"));
cout << endl;
if (isi == eos)
cout << "end of input stream reached" << endl;
else
cout << "eos not reached" << endl;
string str;
if (cin >> str)
cout << "string read from cin: " << str << endl;
else
cout << "Unable to read a string from cin" << endl;
cout << endl;
vector<string> d(isi, eos);
cout << "Displaying the input second time:" << endl;
cout << "---------------------------------" << endl;
copy(d.begin(),
d.end(),
ostream_iterator<string>(cout, "\n"));
return EXIT_SUCCESS;
}
When I run this program with the following input
1
2
3
the following is printed:
Displaying input contents:
--------------------------
1
2
3
eos not reached
Unable to read a string from cin
Displaying the input second time:
---------------------------------
1
Kindly correct me whereever I am wrong in the following:
In the above program, consider the statement:
vector<string> c(isi, eos);
This vector ctor will construct the object 'c' with three input values
namely '1', '2', '3' which were entered from the standard input. After
this ctor statement, there will NOT be any input contents left in cin.
Am I correct ?
The first 'copy' algorithm will copy the contents of 'c' onto the
standard output. Now consider the statement
if (isi == eos)
Here I thought 'isi' should be equal to 'eos' because there is no
content left in 'cin'. But on the contrary, 'eos not reached' is
printed. I am unable to understand the reason for this.
Now consider the statement
if (cin >> str)
For this, the control goes to the else part(because there is no
content left in the stanadard input stream 'cin') and prints:
Unable to read a string from cin
This is as expected.
Now consder the vector object:
vector<string> d(isi, eos);
Since there is no content left in 'cin', the vector object 'd' should
be empty I thought. However, on the contrary, the subsequent 'copy'
algorithm prints:
1
I am unable to understand how the second vector object d's ctor was
able to read 'isi' to fetch the value '1'. In other words, where does
this value '1' come from ?
Kindly explain.
Thanks
V.Subramanian