3.4. Introducing Iterators3.4. 迭代器简介While we can use subscripts to access the elements in a vector, the library also gives us another way to examine elements: We can use an iterator. An iterator is a type that lets us examine the elements in a container and navigate from one element to another. 除了使用下标来访问 vector 对象的元素外,标准库还提供了另一种访问元素的方法:使用迭代器(iterator)。迭代器是一种检查容器内元素并遍历元素的数据类型。 The library defines an iterator type for each of the standard containers, including vector. Iterators are more general than subscripts: All of the library containers define iterator types, but only a few of them support subscripting. Because iterators are common to all containers, modern C++ programs tend to use iterators rather than subscripts to access container elements, even on types such as vector that support subscripting. 标准库为每一种标准容器(包括 vector)定义了一种迭代器类型。迭代器类型提供了比下标操作更通用化的方法:所有的标准库容器都定义了相应的迭代器类型,而只有少数的容器支持下标操作。因为迭代器对所有的容器都适用,现代 C++ 程序更倾向于使用迭代器而不是下标操作访问容器元素,即使对支持下标操作的 vector 类型也是这样。 The details of how iterators work are discussed in Chapter 11, but we can use them without understanding them in their full complexity. 第十一章将详细讨论迭代器的工作原理,但使用迭代器并不需要完全了解它复杂的实现细节。 Container iterator Type容器的 iterator 类型Each of the container types, such as vector, defines its own iterator type: 每种容器类型都定义了自己的迭代器类型,如 vector: vector<int>::iterator iter; This statement defines a variable named iter, whose type is the type named iterator defined by vector<int>. Each of the library container types defines a member named iterator that is a synonym for the actual type of its iterator. 这符语句定义了一个名为 iter 的变量,它的数据类型是 vector<int> 定义的 iterator 类型。每个标准库容器类型都定义了一个名为 iterator 的成员,这里的 iterator 与迭代器实际类型的含义相同。
The begin and end Operationsbegin 和 end 操作Each container defines a pair of functions named begin and end that return iterators. The iterator returned by begin refers to the first element, if any, in the container: 每种容器都定义了一对命名为 begin 和 end 的函数,用于返回迭代器。如果容器中有元素的话,由 begin 返回的迭代器指向第一个元素: vector<int>::iterator iter = ivec.begin(); This statement initializes iter to the value returned by the vector operation named begin. Assuming the vector is not empty, after this initialization, iter refers to the same element as ivec[0]. 上述语句把 iter 初始化为由名为 vector 操作返回的值。假设 vector 不空,初始化后,iter 即指该元素为 ivec[0]。 The iterator returned by the end operation is an iterator positioned "one past the end" of the vector. It is often referred to as the off-the-end iterator indicating that it refers to a nonexistent element "off the end" of the vector. If the vector is empty, the iterator returned by begin is the same as the iterator returned by end. 由 end 操作返回的迭代器指向 vector 的“末端元素的下一个”。“超出末端迭代器”(off-the-end iterator)。表明它指向了一个不存在的元素。如果 vector 为空,begin 返回的迭代器与 end 返回的迭代器相同。
Dereference and Increment on vector Iteratorsvector 迭代器的自增和解引用运算The operations on iterator types let us retrieve the element to which an iterator refers and let us move an iterator from one element to another. 迭代器类型定义了一些操作来获取迭代器所指向的元素,并允许程序员将迭代器从一个元素移动到另一个元素。 Iterator types use the dereference operator (the * operator) to access the element to which the iterator refers: 迭代器类型可使用解引用操作符(dereference operator)(*)来访问迭代器所指向的元素: *iter = 0; The dereference operator returns the element that the iterator currently denotes. Assuming iter refers to the first element of the vector, then *iter is the same element as ivec[0]. The effect of this statement is to assign 0 to that element. 解引用操作符返回迭代器当前所指向的元素。假设 iter 指向 vector 对象 ivec 的第一元素,那么 *iter 和 ivec[0] 就是指向同一个元素。上面这个语句的效果就是把这个元素的值赋为 0。 Iterators use the increment operator (++) (Section 1.4.1, p. 13) to advance an iterator to the next element in the container. Incrementing an iterator is a logically similar operation to the increment operator when applied to int objects. In the case of ints, the effect is to "add one" to the int's value. In the case of iterators, the effect is to "advance the iterator by one position" in the container. So, if iter refers to the first element, then ++iter denotes the second element. 迭代器使用自增操作符(1.4.1 节)向前移动迭代器指向容器中下一个元素。从逻辑上说,迭代器的自增操作和 int 型对象的自增操作类似。对 int 对象来说,操作结果就是把 int 型值“加 1”,而对迭代器对象则是把容器中的迭代器“向前移动一个位置”。因此,如果 iter 指向第一个元素,则 ++iter 指向第二个元素。
Other Iterator Operations迭代器的其他操作Another pair of useful operations that we can perform on iterators is comparison: Two iterators can be compared using either == or !=. Iterators are equal if they refer to the same element; they are unequal otherwise. 另一对可执行于迭代器的操作就是比较:用 == 或 != 操作符来比较两个迭代器,如果两个迭代器对象指向同一个元素,则它们相等,否则就不相等。 A Program that Uses Iterators迭代器应用的程序示例Assume we had a vector<int> named ivec and we wanted to reset each of its elements to zero. We might do so by using a subscript: 假设已声明了一个 vector<int> 型的 ivec 变量,要把它所有元素值重置为 0,可以用下标操作来完成: // reset all the elements in ivec to 0 for (vector<int>::size_type ix = 0; ix != ivec.size(); ++ix) ivec[ix] = 0; This program uses a for loop to iterate through the elements in ivec. The for defines an index, which it increments on each iteration. The body of the for sets each element in ivec to zero. 上述程序用 for 循环遍历 ivec 的元素,for 循环定义了一个索引 ix ,每循环迭代一次 ix 就自增 1。for 循环体将 ivec 的每个元素赋值为 0。 A more typical way to write this loop would use iterators: 更典型的做法是用迭代器来编写循环: // equivalent loop using iterators to reset all the elements in ivec to 0 for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) *iter = 0; // set element to which iter refers to 0 The for loop starts by defining iter and initializing it to refer to the first element in ivec. The condition in the for tests whether iter is unequal to the iterator returned by the end operation. Each iteration increments iter. The effect of this for is to start with the first element in ivec and process in sequence each element in the vector. Eventually, iter will refer to the last element in ivec. After we process the last element and increment iter, it will become equal to the value returned by end. At that point, the loop stops. for 循环首先定义了 iter,并将它初始化为指向 ivec 的第一个元素。for 循环的条件测试 iter 是否与 end 操作返回的迭代器不等。每次迭代 iter 都自增 1,这个 for 循环的效果是从 ivec 第一个元素开始,顺序处理 vector 中的每一元素。最后, iter 将指向 ivec 中的最后一个元素,处理完最后一个元素后,iter 再增加 1,就会与 end 操作的返回值相等,在这种情况下,循环终止。 The statement in the for body uses the dereference operator to access the value of the current element. As with the subscript operator, the value returned by the dereference operator is an lvalue. We can assign to this element to change its value. The effect of this loop is to assign the value zero to each element in ivec. for 循环体内的语句用解引用操作符来访问当前元素的值。和下标操作符一样,解引用操作符的返回值是一个左值,因此可以对它进行赋值来改变它的值。上述循环的效果就是把 ivec 中所有元素都赋值为 0。 Having walked through the code in detail, we can see that this program has exactly the same effect as the version that used subscripts: We start at the first element in the vector and set each element in the vector to zero. 通过上述对代码的详细分析,可以看出这段程序与用下标操作符的版本达到相同的操作效果:从 vector 的第一个元素开始,把 vector 中每个元素都置为 0。
const_iteratorThe previous program used a vector::iterator to change the values in the vector. Each container type also defines a type named const_iterator, which should be used when reading, but not writing to, the container elements. 前面的程序用 vector::iterator 改变 vector 中的元素值。每种容器类型还定义了一种名为 const_iterator 的类型,该类型只能用于读取容器内元素,但不能改变其值。 When we dereference a plain iterator, we get a nonconst reference (Section 2.5, p. 59) to the element. When we dereference a const_iterator, the value returned is a reference to a const (Section 2.4, p. 56) object. Just as with any const variable, we may not write to the value of this element. 当我们对普通 iterator 类型解引用时,得到对某个元素的非 const(2.5 节)。而如果我们对 const_iterator 类型解引用时,则可以得到一个指向 const 对象的引用(2.4 节),如同任何常量一样,该对象不能进行重写。 For example, if text is a vector<string>, we might want to traverse it, printing each element. We could do so as follows: 例如,如果 text 是 vector<string> 类型,程序员想要遍历它,输出每个元素,可以这样编写程序: // use const_iterator because we won't change the elements for (vector<string>::const_iterator iter = text.begin(); iter != text.end(); ++iter) cout << *iter << endl; // print each element in text This loop is similar to the previous one, except that we are reading the value from the iterator, not assigning to it. Because we read, but do not write, through the iterator, we define iter to be a const_iterator. When we dereference a const_iterator, the value returned is const. We may not assign to an element using a const_iterator: 除了是从迭代器读取元素值而不是对它进行赋值之外,这个循环与前一个相似。由于这里只需要借助迭代器进行读,不需要写,这里把 iter 定义为 const_iterator 类型。当对 const_iterator 类型解引用时,返回的是一个 const 值。不允许用 const_iterator: 进行赋值 for (vector<string>::const_iterator iter = text.begin(); iter != text.end(); ++ iter) *iter = " "; // error: *iter is const When we use the const_iterator type, we get an iterator whose own value can be changed but that cannot be used to change the underlying element value. We can increment the iterator and use the dereference operator to read a value but not to assign to that value. 使用 const_iterator 类型时,我们可以得到一个迭代器,它自身的值可以改变,但不能用来改变其所指向的元素的值。可以对迭代器进行自增以及使用解引用操作符来读取值,但不能对该元素赋值。 A const_iterator should not be confused with an iterator that is const. When we declare an iterator as const we must initialize the iterator. Once it is initialized, we may not change its value: 不要把 const_iterator 对象与 const 的 iterator 对象混淆起来。声明一个 const 迭代器时,必须初始化迭代器。一旦被初始化后,就不能改变它的值: vector<int> nums(10); // nums is nonconst const vector<int>::iterator cit = nums.begin(); *cit = 1; // ok: cit can change its underlying element ++cit; // error: can't change the value of cit A const_iterator may be used with either a const or nonconst vector, because it cannot write an element. An iterator that is const is largely useless: Once it is initialized, we can use it to write the element it refers to, but cannot make it refer to any other element. const_iterator 对象可以用于 const vector 或非 const vector,因为不能改写元素值。const 迭代器这种类型几乎没什么用处:一旦它被初始化后,只能用它来改写其指向的元素,但不能使它指向任何其他元素。 const vector<int> nines(10, 9); // cannot change elements in nines // error: cit2 could change the element it refers to and nines is const const vector<int>::iterator cit2 = nines.begin(); // ok: it can't change an element value, so it can be used with a const vector<int> vector<int>::const_iterator it = nines.begin(); *it = 10; // error: *it is const ++it; // ok: it isn't const so we can change its value
// an iterator that cannot write elements vector<int>::const_iterator // an iterator whose value cannot change const vector<int>::iterator
3.4.1. Iterator Arithmetic3.4.1. 迭代器的算术操作In addition to the increment operator, which moves an iterator one element at a time, vector iterators (but few of the other library container iterators) also support other arithmetic operations. These operations are referred to as iterator arithmetic, and include: 除了一次移动迭代器的一个元素的增量操作符外,vector 迭代器(其他标准库容器迭代器很少)也支持其他的算术操作。这些操作称为迭代器算术操作(iterator arithmetic),包括:
We can use iterator arithmetic to move an iterator to an element directly. For example, we could locate the middle of a vector as follows: 可以用迭代器算术操作来移动迭代器直接指向某个元素,例如,下面语句直接定位于 vector 中间元素: vector<int>::iterator mid = vi.begin() + vi.size() / 2; This code initializes mid to refer to the element nearest to the middle of ivec. It is more efficient to calculate this iterator directly than to write an equivalent program that increments the iterator one by one until it reaches the middle element. 上述代码用来初始化 mid 使其指向 vi 中最靠近正中间的元素。这种直接计算迭代器的方法,与用迭代器逐个元素自增操作到达中间元素的方法是等价的,但前者的效率要高得多。
![]() |