如何STL返回2个迭代器具有相同的功能? [英] How does STL return 2 iterators with the same function?
问题描述
我注意到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屋!