📄 iterators.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta name="generator" content="HTML Tidy for Linux/x86 (vers 1st October 2002), see www.w3.org"> <title>C++ Iterators</title> </head> <body bgcolor="#ffffff"> <table width="100%" bgcolor="#eeeeff"> <tr> <td><a href="index.html">cppreference.com</a> -> C++ Iterators</td> </tr> </table> <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_details.html">vector</a>. There are several different types of iterators:</p> <table align="center" width="75%"> <tr> <th>Iterator</th> <th>Description</th> </tr> <tr bgcolor="#eeeeff"> <td>input_iterator</td> <td>Read values with forward movement. These can be incremented, compared, and dereferenced.</td> </tr> <tr> <td>output_iterator</td> <td>Write values with forward movement. These can be incremented and dereferenced.</td> </tr> <tr bgcolor="#eeeeff"> <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> <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 bgcolor="#eeeeff"> <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> <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. </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -