& vector [0]和vector.begin()有什么区别? [英] What is the difference between &vector[0] and vector.begin()?

查看:148
本文介绍了& vector [0]和vector.begin()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与有效的stl书的第16项相关,该条款指出,在使用vector(让我们假设 vector< int> vec )而不是传统代码中的数组的情况下,我们必须使用& vec [0]而不是vec.begin():

  void doSomething(const int * pInts,size_t numlnts);做某事(& vec [0],vec.size());\\正确的!!dosomething(vec.begin(),vec.size());\\ 错误的!!为什么??? 

该书指出 vec.begin()& vec [0] 不同.为什么 ?两者之间有什么区别?

解决方案

A std ::向量是封装动态大小数组的序列容器.这使您可以方便地存储一堆元素,而不必担心管理作为元素存储的基础数组.使用这些类的大部分便利来自于以下事实:它们为您提供了一堆方法,使您无需处理原始指针即可处理序列,而迭代器就是一个例子.

& vec [0] 是指向向量正在使用的基础存储的第一个元素的指针. vec.begin()迭代器,其起始于向量.虽然这两种方法都为您提供了一种顺序访问元素的方式,但它们是2个不同的概念.搜索迭代器,以更好地了解其工作原理.

如果您的代码支持迭代器,则通常最容易使用迭代器来迭代数据.造成这种情况的部分原因是迭代器不是指针,它们使您可以迭代数据结构的元素,而无需了解要迭代的数据结构的实现细节./p>

但是,有时您需要原始的项目数组,例如在某些旧版API或对C代码的调用中,您可能需要将指针传递给该数组.在这种情况下,您别无选择,只能从向量中提取原始数组,可以使用诸如& vec [0] 之类的方法进行.请注意,如果您具有c ++ 11支持,则可以使用 std :: vector :: data ,这将使您能够访问基础存储阵列.c ++ 11方式的另一个好处是,还可以更清楚地向阅读您的代码的人说明您的意图.

This question is related with item 16 of effective stl book which states that while using vector(lets assume vector<int>vec) instead of array in a legacy code we must use &vec[0] instead of vec.begin() :

 void doSomething(const int* pInts, size_t numlnts);  
 dosomething(&vec[0],vec.size()); \\correct!! 
 dosomething(vec.begin(),vec.size()); \\ wrong!! why??? 

The book states that vec.begin() is not same as &vec[0] . Why ? What the difference between the two ?

解决方案

A std::vector is sequence container that encapsulates dynamic size arrays. This lets you conveniently store a bunch of elements without needing to be as concerned with managing the underlying array that is the storage for your elements. A large part of the convenience of using these classes comes from the fact that they give you a bunch of methods that let you deal with the sequence without needing to deal with raw pointers, an iterator is an example of this.

&vec[0] is a pointer to the first element of the underlying storage that the vector is using. vec.begin() is an iterator that starts at the beginning of the vector. While both of these give you a way to access the elements in the sequence these are 2 distinct concepts. Search up iterators to get a better idea of how this works.

If your code supports iterators its often easiest to use the iterators to iterate over the data. Part of the reasons for this is that iterators are not pointers, they let you iterate over the elements of the data structure without needing to know as much about the implementation details of the datastructure you are iterating over.

However sometimes you need the raw array of items, for example in some legacy API's or calls to C code you might need to pass a pointer to the array. In this case you have no choice but to extract the raw array from the vector, you can do this using something such as &vec[0]. Note that if you have c++11 support there's an explicit way to do this with std::vector::data which will give you access to the underlying storage array. The c++11 way has the additional benefit of also more clearly stating your intent to the people reading your code.

这篇关于&amp; vector [0]和vector.begin()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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