C++ Primer Fifth Edition 英文彩色带书签 http://download.csdn.net/detail/kingeasternsun/5529053
C++ Primer Plus (6th Edition) 英文原版 彩色带书签http://download.csdn.net/detail/kingeasternsun/5508691
map 对象的元素是键-值对,也即每个元素包含两个部分:键以及由键关联的值。map 的value_type 就反映了这个事实。该类型比前面介绍的容器所使用的元素类型要复杂得多:value_type 是存储元素的键以及值的pair 类型,而且键为const。例如,word_count 数组的value_type 为pair<const string, int> 类型。
for (std::map<std::string, int>::iterator iter = class_count.begin();iter<class_count.end();iter++){
if (iter->second > maxv)
C = iter->first;
}
error C2678: 二进制“<”: 没有找到接受“std::_Tree_iterator<_Mytree>”类型的左操作数的运算符(或没有可接受的转换)
在判断迭代器的时候,用!=而不是<比较,
for (std::map<std::string, int>::iterator iter = class_count.begin();iter!=class_count.end();iter++){
if (iter->second > maxv)
C = iter->first;
}
同理 我们要注意,在list容器里,迭代器也不支持<操作,比较时用!=
一下是摘抄c++ primer 4rd的段落
Iterators on vector and deque Support Additional Operations
vector 和 deque 容器的迭代器提供额外的运算
There are two important sets of operations that only vector anddeque support: iterator arithmetic (Section 3.4.1, p.100) and the use of the relational operators (in addition to== and !=) to compare two iterators. These operations are summarized inTable 9.4 on the facing page.
C++ 定义的容器类型中,只有 vector 和 deque 容器提供下面两种重要的运算集合:迭代器算术运算(第 3.4.1 节),以及使用除了== 和 != 之外的关系操作符来比较两个迭代器(== 和 != 这两种关系运算适用于所有容器)。表 9.4 总结了这些相关的操作符。
iter + n | Adding (subtracting) an integral value n to (from) an iterator yields an iterator that many elements forward (backward) within the container. The resulting iterator must refer to an element in the container or one past the end of the container. 在迭代器上加(减)整数值 n,将产生指向容器中前面(后面)第 n 个元素的迭代器。新计算出来的迭代器必须指向容器中的元素或超出容器末端的下一位置 |
iter1 += iter2 | Compound-assignment versions of iterator addition and subtraction. Assigns the value of adding or subtractingiter1 and iter2 into iter1. 这里迭代器加减法的复合赋值运算:将 iter1 加上或减去 iter2 的运算结果赋给 iter1 |
iter1 - iter2 | Subtracting two iterators yields the number that when added to the right-hand iterator yields the left-hand iterator. The iterators must refer to elements in the same container or one past the end of the container. 两个迭代器的减法,其运算结果加上右边的迭代器即得左边的迭代器。这两个迭代器必须指向同一个容器中的元素或超出容器末端的下一位置 Supported only for vectorand deque. 只适用于 vector 和 deque 容器 |
>, >=, <, <= | Relational operators on iterators. One iterator is less than another if it refers to an element whose position in the container is ahead of the one referred to by the other iterator. The iterators must refer to elements in the same container or one past the end of the container. 迭代器的关系操作符。当一个迭代器指向的元素在容器中位于另一个迭代器指向的元素之前,则前一个迭代器小于后一个迭代器。关系操作符的两个迭代器必须指向同一个容器中的元素或超出容器末端的下一位置 Supported only for vectorand deque. 只适用于 vector 和 deque 容器 |
The reason that only vector and deque support the relational operators is that onlyvector and deque offer fast, random access to their elements. These containers are guaranteed to let us efficiently jump directly to an element given its position in the container. Because these containers support random access by position, it is possible for their iterators to efficiently implement the arithmetic and relational operations.
关系操作符只适用于 vector 和 deque 容器,这是因为只有这种两种容器为其元素提供快速、随机的访问。它们确保可根据元素位置直接有效地访问指定的容器元素。这两种容器都支持通过元素位置实现的随机访问,因此它们的迭代器可以有效地实现算术和关系运算。
For example, we could calculate the midpoint of a vector as follows:
例如,下面的语句用于计算 vector 对象的中点位置:
vector<int>::iterator iter = vec.begin() + vec.size()/2;
On the other hand, this code
另一方面,代码:
// copy elements from vec into ilist list<int> ilist(vec.begin(), vec.end()); ilist.begin() + ilist.size()/2; // error: no addition on list iterators
is an error. The list iterator does not support the arithmetic operationsaddition or subtractionnor does it support the relational (<=, <, >=, >) operators. It does support pre- and postfix increment and decrement and the equality (inequality) operators.
是错误的。list 容器的迭代器既不支持算术运算(加法或减法),也不支持关系运算(<=, <, >=, >),它只提供前置和后置的自增、自减运算以及相等(不等)运算。