我发现自己写下了很多:
int location =2; vector<int> vec; vector<int>::iterator it=vec.begin(); /..../ std::advance(it,location);
代替
it= it + 5;
什么是首选/推荐方式?
解决方法
添加只适用于随机访问迭代器. std :: advance将与各种迭代器配合使用.只要你只处理迭代器到向量,它没有什么真正的区别,但是std :: advance会使你的代码更加通用(例如,你可以用一个列表来代替这个向量,而这个部分仍然可以使用).
编辑:对于那些关心的人来说,标准描述了进步和距离如下(§24.3.4/ 1):
Since only random access iterators provide + and – operators,the library provides two function templates
advance
anddistance
. These function templates use+
and-
for random access iterators (and are,therefore,constant time for them); for input,forward and bidirectional iterators they use++
to provide linear time implementations.
原文地址:https://www.jb51.cc/c/111712.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。