迭代器
1、迭代器:访问容器元素的一种通用方式,
使用方式:类似于指针的使用方式,可以通过解引用获取元素内容可以通
++,–进行位置的移动
begin迭代器:第一个元素的位置
end迭代器:最后一个元素的下一个位置迭代器的范围左闭右开:[begin,end)可读可写的迭代器: iterator,reverse_iterator
只读迭代器:const_iterator,const_reverse_iterator
begin:
1、iterator begin () ---->非const对象调用
2、const_iterator_begin () const—>const对象调用
end () :
1、iterator end () —>非const对象调用
2、const_iterator_end() const ---->const对象调用rbegin,rend同上,也
有两个接口
cbegin,cend,crbegin,erend—>都返回const迭代器
end, rend, cend, crend不能解引用访问
在范围for:如果变量类型为引用,则可以直接通过变量修改内容
常见的范围for书写形式:
1、只读的: for (const auto&变量:需要遍历的容器)
2、可读可写: for ( auto&变量:需要遍历的容器)
string对象的遍历方式:
1、operstor[]+循环2、迭代器
3、范围for