Team LiB
Previous Section Next Section

3.4. Introducing Iterators

3.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 与迭代器实际类型的含义相同。

Terminology: Iterators and Iterator Types

术语:迭代器和迭代器类型

When first encountered, the nomenclature around iterators can be confusing. In part the confusion arises because the same term, iterator, is used to refer to two things. We speak generally of the concept of an iterator, and we speak specifically of a concrete iterator type defined by a container, such as vector<int>.

程序员首次遇到有关迭代器的术语时可能会困惑不解,原因之一是由于同一个术语 iterator 往往表示两个不同的事物。一般意义上指的是迭代器的概念;而具体而言时指的则是由容器定义的具体的 iterator 类型,如 vector<int>

What's important to understand is that there is a collection of types that serve as iterators. These types are related conceptually. We refer to a type as an iterator if it supports a certain set of actions. Those actions let us navigate among the elements of a container and let us access the value of those elements.

重点要理解的是,有许多用作迭代器的类型,这些类型在概念上是相关的。若一种类型支持一组确定的操作(这些操作可用来遍历容器内的元素,并访问这些元素的值),我们就称这种类型为迭代器。

Each container class defines its own iterator type that can be used to access the elements in the container. That is, each container defines a type named iterator, and that type supports the actions of an (conceptual) iterator.

各容器类都定义了自己的 iterator 类型,用于访问容器内的元素。换句话说,每个容器都定义了一个名为 iterator 的类型,而这种类型支持(概念上的)迭代器的各种操作。


The begin and end Operations

begin 和 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 返回的迭代器相同。

The iterator returned by the end operation does not denote an actual element in the vector. Instead, it is used as a sentinel indicating when we have processed all the elements in the vector.

由 end 操作返回的迭代器并不指向 vector 中任何实际的元素,相反,它只是起一个哨兵(sentinel)的作用,表示我们已处理完 vector 中所有元素。



Dereference and Increment on vector Iterators

vector 迭代器的自增和解引用运算

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 指向第二个元素。

Because the iterator returned from end does not denote an element, it may not be incremented or dereferenced.

由于 end 操作返回的迭代器不指向任何元素,因此不能对它进行解引用或自增操作。



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。

This program, like the one on page 94, is safe if the vector is empty. If ivec is empty, then the iterator returned from begin does not denote any element; it can't, because there are no elements. In this case, the iterator returned from begin is the same as the one returned from end, so the test in the for fails immediately.

本节给出的例子程序和 3.3.2 节 vector 的下标操作的程序一样,如果 vector 为空,程序是安全的。如果 ivec 为空,则 begin 返回的迭代器不指向任何元素——由于没有元素,所以它不能指向任何元素。在这种情况下,从 begin 操作返回的迭代器与从 end 操作返回的迭代器的值相同,因此 for 语句中的测试条件立即失败。



const_iterator

The 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 类型解引用时,得到对某个元素的非 const2.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


Exercises Section 3.4

Exercise 3.17:

Redo the exercises from Section 3.3.2 (p. 96), using iterators rather than subscripts to access the elements in the vector.

重做 3.3.2 节 的习题,用迭代器而不是下标操作来访问 vector 中的元素。

Exercise 3.18:

Write a program to create a vector with 10 elements. Using an iterator, assign each element a value that is twice its current value.

编写程序来创建有 10 个元素的 vector 对象。用迭代器把每个元素值改为当前值的 2 倍。

Exercise 3.19:

Test your previous program by printing the vector.

验证习题 3.18 的程序,输出 vector 的所有元素。

Exercise 3.20:

Explain which iterator you used in the previous programs, and why.

解释一下在上几个习题的程序实现中你用了哪种迭代器,并说明原因。

Exercise 3.21:

When would you use an iterator that is const? When would you use a const_iterator. Explain the difference between them.

何时使用 const 迭代器的?又在何时使用 const_iterator?解释两者的区别。


3.4.1. Iterator Arithmetic

3.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),包括:

  • iter + n

    iter - n

    We can add or subtract an integral value to an iterator. Doing so yields a new iterator positioned n elements ahead of (addition) or behind (subtraction) the element to which iter refers. The result of the addition or subtraction must refer to an element in the vector to which iter refers or to one past the end of that vector. The type of the value added or subtracted ought ordinarily to be the vector's size_type or difference_type (see below).

    可以对迭代器对象加上或减去一个整形值。这样做将产生一个新的迭代器,其位置在 iter 所指元素之前(加)或之后(减) n 个元素的位置。加或减之后的结果必须指向 iter 所指 vector 中的某个元素,或者是 vector 末端的后一个元素。加上或减去的值的类型应该是 vector 的 size_type 或 difference_type 类型(参考下面的解释)。

  • iter1 - iter2

    Computes the difference between two iterators as a value of a signed integral type named difference_type, which, like size_type, is defined by vector. The type is signed because subtraction might have a negative result. This type is guaranteed to be large enough to hold the distance between any two iterators. Both iter1 and iter2 must refer to elements in the same vector or the element one past the end of that vector.

    该表达式用来计算两个迭代器对象的距离,该距离是名为 difference_type 的 signed 类型 size_type 的值,这里的 difference_type 是 signed 类型,因为减法运算可能产生负数的结果。该类型可以保证足够大以存储任何两个迭代器对象间的距离。iter1 与 iter2 两者必须都指向同一 vector 中的元素,或者指向 vector 末端之后的下一个元素。

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 中最靠近正中间的元素。这种直接计算迭代器的方法,与用迭代器逐个元素自增操作到达中间元素的方法是等价的,但前者的效率要高得多。

Any operation that changes the size of a vector makes existing iterators invalid. For example, after calling push_back, you should not rely on the value of an iterator into the vector.

任何改变 vector 长度的操作都会使已存在的迭代器失效。例如,在调用 push_back 之后,就不能再信赖指向 vector 的迭代器的值了。



Exercises Section 3.4.1

Exercise 3.22:

What happens if we compute mid as follows:

如果采用下面的方法来计算 mid 会产生什么结果?

     vector<int>::iterator mid = (vi.begin() + vi.end()) / 2;


    Team LiB
    Previous Section Next Section