如何STL返回2个迭代器具有相同的功能? [英] How does STL return 2 iterators with the same function?

查看:122
本文介绍了如何STL返回2个迭代器具有相同的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到vector.begin()将返回一个const迭代器,或一个基于左边给出的迭代器。

I noticed that vector.begin() will return a const iterator, or an iterator based on what is given on the left. How is something like this implemented since the arguments given to the function are the same.

感谢

推荐答案

它的返回类型是基于向量本身是否通过 const 引用(或指针)访问。另外,迭代器可以隐式转换为 const_iterator ,这就是为什么这样的工作原理: std :: vector< T> v; std :: vector< T> :: const_iterator it = v.begin();

Its return-type is based on whether the vector itself is being accessed through a const reference (or pointer) or not. Also, an iterator can be implicitly converted to a const_iterator, which is why something like this works: std::vector<T> v; std::vector<T>::const_iterator it = v.begin();.

这篇关于如何STL返回2个迭代器具有相同的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆