📄 iistream.htm
字号:
<html>
<head>
<title>istream_iterator</title>
<head>
<body bgcolor="#FFFFFF">
<a name="here"></a>
<img src="itistban.gif">
<pre>
<font size=5>Class Name</font> istream_iterator
<font size=5>Header File</font> <iterator>
<font size=5>Classification</font> abstract data type
</pre>
<a href="iistream.htm#crd">Class Relationship Diagram</a><br>
<br>
<a href="iistream.htm#class-descrip">Class Description</a>
<h1>Member Classes</h1>
<a href="iistream.htm#basic-istream">basic_istream<charT,traits>*</a>
<a href="iistream.htm#value">T value</a>
<h1>Methods</h1>
<pre>
<a href="iistream.htm#istream1">istream_iterator();</a>
<a href="iistream.htm#istream2">istream_iterator(istream_type& s);</a>
<a href="iistream.htm#istream3">istream_iterator(const istream_iterator<T,charT,traits,Distance>& x);</a>
<a href="iistream.htm#~istream">~istream_iterator();</a>
</pre>
<h1>Operators</h1>
<pre>
<a href="iistream.htm#operator*"> const T& operator*() const;</a>
<a href="iistream.htm#operator++1">istream_iterator<T,charT,traits,Distance>& operator++();</a>
<a href="iistream.htm#operator++2">istream_iterator<T,charT,traits,Distance> operator++(int);</a>
</pre>
<a href="iistream.htm#example"><h2>Example</h2></a>
<hr>
<a name="class-descrip"><h3>Class Description</h3>
<p>
The istream_iterator is an <a href="defines.htm#io">input iterator</a>. This object is derived from the class template, iterator. Using the operator>>, values of
class T are successively read from an input stream. The elements are read from the input
stream successively moving in a single direction. Only one pass is made over the data.
An end-of-stream iterator object is created when an istream_iterator object is
constructed using the constructor with no arguments (istream_iterator()). The end-of-stream
is reached when the iterator is equal to end-of-stream iterator value.
When the iterator object is incremented using the ++ operator, a value of class T is read from
the input stream and stored within the object. When the iterator is dereferenced, this value is
returned. Each time a new value of class T is read from the
input, the current value is discarded and the new value is stored.<br>
<br>
Note: The result of operator * or operator-< on an end-of-stream is not defined. The operator++ is not equality-preserving. If i == j, there is no guarantee that when i and j are incremented they will still be equal (++i != ++j).
</p>
<hr>
<a name="basic-istream"><h3>basic_istream</h3>
<p>
basic_istream is the the input stream in which the elements are read from successively
moving in a single direction.
</p>
<hr>
<a name="value"><h3>T value</h3>
<p>
value is the data type the iterator points to.
</p>
<hr>
<pre>
<a name="istream1">
Method istream_iterator()</a>
Access Public
Classification Constructor
Syntax istream_iterator();
Parameters None
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an end-of-stream iterator object.
</p>
<hr>
<pre>
<a name="istream2">
Method istream_iterator()</a>
Access Public
Classification Constructor
Syntax istream_iterator(istream_type& s);
Parameters <em>s</em> is the iterator initialized with the reference to an
input stream in which type T values are extracted.
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an istream_iterator object that extracts a value of type T
from the input stream specified in the <em>s</em>.
</p>
<hr>
<pre>
<a name="istream3">
Method istream_iterator()</a>
Access Public
Classification Constructor
Syntax istream_iterator(const istream_iterator<T,charT,traits,
Distance>& x);
Parameters <em>X</em> is a refererence to a const istream_iterator object
in which the newly constructed object will be a copy.
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an istream_iterator object which is a copy of <em>x</em>.
</p>
<hr>
<pre>
<a name="~istream">
Method ~istream_iterator()
Access Public
Classification Constructor
Syntax ~istream_iterator();
Parameters None
Returns None
</pre>
<h3>Description</h3>
<p>
The destructor destroys the iterator object.
</p>
<hr>
<pre>
<a name="operator*">
Method operator*()</a>
Access Public
Classification Accessor
Syntax const T& operator*() const;
Parameters None
Returns This operator returns a reference to const T.
It is not defined what will be returned if the
dereference operator is used on an end-of-stream.
</pre>
<h3>Description</h3>
<p>
The deference operator dereferences a const iterator object and returns a
reference to const T. This prevents the returned element to be used to
write values to the input stream.
</p>
<hr>
<pre>
<a name="operator++1">
Method operator++()</a>
Access Public
Classification Modifier
Syntax istream_iterator<T,charT,traits,Distance>& operator++();
Parameters None
Returns This method returns a reference to the istream_iterator
object (*this).
</pre>
<h3>Description</h3>
<p>
The operator++() reads from the input stream and stores a value of type T
each time it is called.
</p>
<hr>
<pre>
<a name="operator++2">
Method operator++()</a>
Access Public
Classification Modifier
Syntax istream_iterator<T,charT,traits,Distance>&
operator++(int x);
Parameters <em>x</em> determines how many values of type T will be read
and stored.
Returns This operator returns a reference to istream_iterator
object.
</pre>
<h3>Description</h3>
<p>
This operator++() is the incrementation operator. It reads from the input
stream and stores as many values of type T as specified by <em>x</em>.
</p>
<hr>
<a name="crd"><h2>The Class Relationship Diagram for istream_object</h2></a>
<img src="iistream.gif">
<hr>
<a name="example"></a>
<img src="lego.gif">
<pre>
1 #include <iterator>
2 #include <string>
3 #include <fstream>
4 #include <deque>
5 #include <list>
6 #include <algorithm>
7 #include <iostream>
8
9 using namespace std;
10
11 class discard{
12 list<string> Keys;
13 static float Num;
14 public:
15 discard(void);
16 int operator()(string X);
17 float number(void);
18 };
19
20 float discard::Num = 0;
21
22 discard::discard(void)
23 {
24
25 ifstream Keyfile("key8-1.txt");
26 istream_iterator<string> Inkey(Keyfile);
27 istream_iterator<string> eof;
28 copy(Inkey,eof,back_inserter(Keys));
29 Keyfile.close();
30
31 }
32
33 int discard::operator()(string X)
34 {
35
36 int Temp = 0;
37 Temp = count(Keys.begin(),Keys.end(),X);
38 discard::Num = discard::Num + Temp;
39 return Temp;
40
41 }
42
43 float discard::number(void)
44 {
45
46 return(discard::Num);
47
48 }
49
50
51 void main(void)
52 {
53
54 float Percent = 0;
55 discard Discard;
56 ifstream Infile("in8-1.txt");
57 ofstream Outfile("out8-1.txt");
58 istream_iterator<string> In(Infile),Last;
59 ostream_iterator<string> Out(Outfile," ");
60 deque<string> Dec;
61 copy(In,Last,front_inserter(Dec));
62 remove_copy_if(Dec.begin(), Dec.end(),Out, Discard);
63 cout.precision(2);
64 Percent = (Discard.number() / Dec.size()) * 10;
65 cout << Percent << "%";
66 Infile.close();
67 Outfile.close();
68
69 }
70
</pre>
<hr>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -