C ++ std :: vector<> :: iterator不是指针,为什么? [英] C++ std::vector<>::iterator is not a pointer, why?

查看:200
本文介绍了C ++ std :: vector<> :: iterator不是指针,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需一点介绍,用简单的话说。
在C ++中,迭代器是东西,你可以在其上编写至少dereference运算符 * it ,增量运算符 ++ it ,对于更高级的双向迭代器,递减 - 它,最后但并非最不重要的是,对于随机访问迭代器,我们需要运算符索引 it [] 以及可能的加法和减法。

Just a little introduction, with simple words. In C++, iterators are "things" on which you can write at least the dereference operator *it, the increment operator ++it, and for more advanced bidirectional iterators, the decrement --it, and last but not least, for random access iterators we need operator index it[] and possibly addition and subtraction.

C ++中的这类东西是具有相应运算符重载的类型的对象,或简单和简单的指针。

Such "things" in C++ are objects of types with the according operator overloads, or plain and simple pointers.

std :: vector<> 是一个包装连续的容器类数组,所以指针作为迭代器是有道理的。在网上,在一些文献中你可以找到 vector.begin()用作指针。

std::vector<> is a container class that wraps a continuous array, so pointer as iterator makes sense. On the nets, and in some literature you can find vector.begin() used as a pointer.

使用指针的基本原理是开销更少,性能更高,特别是如果优化编译器检测到迭代并执行其操作(向量指令和内容)。使用迭代器可能更难以使编译器进行优化。

The rationale for using a pointer is less overhead, higher performance, especially if an optimizing compiler detects iteration and does its thing (vector instructions and stuff). Using iterators might be harder for the compiler to optimize.

知道这一点,我的问题是为什么现代STL实现,比如Mingw 4.7中的MSVC ++ 2013或libstdc ++,使用特殊的向量迭代器的类?

Knowing this, my question is why modern STL implementations, let's say MSVC++ 2013 or libstdc++ in Mingw 4.7, use a special class for vector iterators?

推荐答案

你完全正确 vector :: iterator 可以通过简单的指针实现(参见此处) - in事实上,迭代器的概念基于指向数组元素的指针。对于其他容器,例如 map list deque ,但是,指针根本不起作用。那为什么不这样做呢?以下是为什么类实现比原始指针更可取的三个原因。

You're completely correct that vector::iterator could be implemented by a simple pointer (see here) -- in fact the concept of an iterator is based on that of a pointer to an array element. For other containers, such as map, list, or deque, however, a pointer won't work at all. So why is this not done? Here are three reasons why a class implementation is preferrable over a raw pointer.


  1. 将迭代器实现为单独的类型允许其他功能(超出标准要求的范围),例如(在Quentins注释之后的编辑中添加)在解除引用迭代器时添加断言的可能性,例如,在调试模式下。

  1. Implementing an iterator as separate type allows additional functionality (beyond what is required by the standard), for example (added in edit following Quentins comment) the possibility to add assertions when dereferencing an iterator, for example, in debug mode.

重载分辨率如果迭代器是一个指针 T * ,它可以作为有效参数传递给一个函数取 T * ,而迭代器类型则无法实现。因此,使 std :: vector<> :: iterator 指针实际上改变了现有代码的行为。例如,考虑

overload resolution If the iterator were a pointer T*, it could be passed as valid argument to a function taking T*, while this would not be possible with an iterator type. Thus making std::vector<>::iterator a pointer in fact changes the behaviour of existing code. Consider, for example,

template<typename It>
void foo(It begin, It end);
void foo(const double*a, const double*b, size_t n=0);

std::vector<double> vec;
foo(vec.begin(), vec.end());    // which foo is called?


  • 依赖于参数的查询(ADL;由juanchopanza指出)如果进行非限定调用,ADL将确保仅当参数是 namespace std namespace std 中的函数。 C $ C>。所以,

  • argument-dependent lookup (ADL; pointed out by juanchopanza) If you make an unqualified call, ADL ensures that functions in namespace std will be searched only if the arguments are types defined in namespace std. So,

    std::vector<double> vec;
    sort(vec.begin(), vec.end());             // calls std::sort
    sort(vec.data(), vec.data()+vec.size());  // fails to compile
    

    std :: sort vector<> :: iterator 仅仅是一个指针,则找不到。

    std::sort is not found if vector<>::iterator were a mere pointer.

    这篇关于C ++ std :: vector&lt;&gt; :: iterator不是指针,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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