-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterators.html
More file actions
128 lines (103 loc) · 4.05 KB
/
iterators.html
File metadata and controls
128 lines (103 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/iterators.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:06 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<title>C++ Iterators</title>
<link href="cppreference.css" rel="stylesheet" type="text/css">
<link href="prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify.js"></script>
</head>
<body onload="prettyPrint()">
<table>
<tr>
<td>
<div class="body-content">
<div class="header-box">
<a href="index-2.html">cppreference.com</a> > C++ Iterators
</div>
<h3>C++ Iterators</h3>
<p>Iterators are used to access members of the container classes, and
can be used in a similar manner to pointers. For example, one might
use an iterator to step through the elements of a <a href=
"cppvector/index.html">vector</a>. There are several different types
of iterators:</p>
<table class="misc-table">
<tr class="misc-table-tr-1">
<th>Iterator</th>
<th>Description</th>
</tr>
<tr class="misc-table-tr-2">
<td>input_iterator</td>
<td>Read values with forward movement. These can be incremented,
compared, and dereferenced.</td>
</tr>
<tr class="misc-table-tr-1">
<td>output_iterator</td>
<td>Write values with forward movement. These can be incremented
and dereferenced.</td>
</tr>
<tr class="misc-table-tr-2">
<td>forward_iterator</td>
<td>Read or write values with forward movement. These combine the
functionality of input and output iterators with the ability to
store the iterators value.</td>
</tr>
<tr class="misc-table-tr-1">
<td>bidirectional_iterator</td>
<td>Read and write values with forward and backward movement.
These are like the forward iterators, but you can increment and
decrement them.</td>
</tr>
<tr class="misc-table-tr-2">
<td>random_iterator</td>
<td>Read and write values with random access. These are the most
powerful iterators, combining the functionality of bidirectional
iterators with the ability to do pointer arithmetic and pointer
comparisons.</td>
</tr>
<tr class="misc-table-tr-1">
<td>reverse_iterator</td>
<td>Either a random iterator or a bidirectional iterator that
moves in reverse direction.</td>
</tr>
</table>
<p>Each of the container classes is associated with a type of
iterator, and each of the STL algorithms uses a certain type of
iterator. For example, vectors are associated with
<strong>random-access iterators</strong>, which means that they can
use algorithms that require random access. Since random-access
iterators encompass all of the characteristics of the other
iterators, vectors can use algorithms designed for other iterators as
well.</p>
<p>The following code creates and uses an iterator with a vector:</p>
<pre>
vector<int> the_vector;
vector<int>::iterator the_iterator;
for( int i=0; i < 10; i++ )
the_vector.push_back(i);
int total = 0;
the_iterator = the_vector.begin();
while( the_iterator != the_vector.end() ) {
total += *the_iterator;
the_iterator++;
}
cout << "Total=" << total << endl;
</pre>Notice that you can access the elements of the container by
dereferencing the iterator.
</div>
</td>
<script src="../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/iterators.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:06 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>