C ++向量迭代器与指针 [英] C++ vector iterators vs. pointers

查看:106
本文介绍了C ++向量迭代器与指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻址向量元素的方法有很多.

There are so many alternative ways of addressing elements of a vector.

我可以这样使用指针:

vector<int> v = {10, 11, 12};
int *p = &v[0];
cout << *p;    //Outputs "10"

我也可以这样使用指针:

I could use a pointer this way too:

vector<int> v = {10, 11, 12};
vector<int>::pointer p = v.data();
cout << *p;    //Outputs "10"

我还可以使用迭代器类型:

I could also use the iterator type:

vector<int> v = {10, 11, 12};
vector<int>::iterator i = v.begin();
cout << *i;    //Outputs "10"

我在这里缺少任何重大区别吗?

Are there any significant differences that I'm missing here?

推荐答案

就能够执行手头的任务而言,它们都可以正常工作.毕竟,它们都提供了满足迭代器要求的对象,并且您正在使用它们指向vector的相同元素. 但是,我会选择vector<int>::iterator选项,因为该类型更能表达我们打算如何使用它.

As far as being able to perform the task at hand, they all work equally well. After all, they all provide an object which meets the requirements of an iterator and you are using them to point at the same element of the vector. However, I would pick the vector<int>::iterator option because the type is more expressive about how we intend to use it.

原始指针类型int*几乎不告诉您p是什么,只是它存储了int的地址.如果单独考虑p,它的类型并不能告诉您如何使用它. vector<int>::pointer选项具有相同的问题-它只是将其指向的对象的类型表示为向量的元素类型.它实际上没有必要指向vector.

The raw pointer type, int*, tells you very little about what p is, except that it stores the address of an int. If you think about p in isolation, its type doesn't tell you very much about how you can use it. The vector<int>::pointer option has the same issue - it just expresses the type of the objects it points at as being the element type of a vector. There's no reason it actually needs to point into a vector.

另一方面,vector<int>::iterator告诉您所有您需要了解的内容.它明确指出该对象是一个迭代器,并且该迭代器用于指向vector<int>中的元素.

On the other hand vector<int>::iterator tells you everything you need to know. It explicitly states that the object is an iterator and that iterator is used to point at elements in a vector<int>.

如果碰巧更改了容器类型,这还具有易于维护的优点.例如,如果更改为std::list,则指针类型将不再起作用,因为元素未存储为连续数组.容器的iterator类型始终为您提供一种可用于迭代其元素的类型.

This also has the benefit of being more easily maintainable if you ever happen to change the container type. If you changed to a std::list, for example, the pointer type just wouldn't work any more because the elements are not stored as a contiguous array. The iterator type of a container always provides you with a type you can use to iterate over its elements.

当我们有了Concepts时,我希望最佳实践是这样的:

When we have Concepts, I'd expect the best practise to be something like:

ForwardIteratorOf<int> it = std::begin(v);

其中ForwardIteratorOf<int>(我想像中存在)已更改为最能描述您对it意图的概念.如果元素的类型无关紧要,则只需ForwardIterator(或BidirectionalIteratorRandomAccessIterator等).

where ForwardIteratorOf<int> (which I am imagining exists) is changed to whatever concept best describes your intentions for it. If the type of the elements doesn't matter, then just ForwardIterator (or BidirectionalIterator, RandomAccessIterator, or whatever).

这篇关于C ++向量迭代器与指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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