c – 用于std :: vector std :: advance VS运算符的Advance迭代器

c – 用于std :: vector std :: advance VS运算符的Advance迭代器,第1张

概述我发现自己写下了很多: int location =2;vector<int> vec;vector<int>::iterator it=vec.begin();/..../std::advance(it, location); 代替 it= it + 5; 什么是首选/推荐方式? 添加只适用于随机访问迭代器. std :: advance将与各种迭代器配合使用.只要你只处理迭代器到向量 我发现自己写下了很多:
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 and distance. 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.

总结

以上是内存溢出为你收集整理的c – 用于std :: vector std :: advance VS运算符的Advance迭代器全部内容,希望文章能够帮你解决c – 用于std :: vector std :: advance VS运算符的Advance迭代器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1255200.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存