The function requires a pointer to an array const int* as a parameter. It is required to transfer a vector v<int> .

Are the following transfers equivalent: &v[0] and v.begin() ?

  • The question is why you have such a strange function. Redo it, business something. - VladD
  • The @VladD function may require a pointer, since written in c for example. Or there is already a working interface. And std::vector pretty easy to make friends with the native pointer where necessary. - 伪位蔚蠂慰位蠀蟿
  • @alexolut: Well then, don't use this function. It is necessary to rewrite it in a template version, so that it takes two iterators. Then there will be no problems with <s> owl stringing </ s> data type conversion. - VladD
  • @VladD You do not understand, apparently. Suppose there is a library (only in binary form), and one of the functions in this library takes const int* . You cannot rewrite this function, or anything at all in this library. - 伪位蔚蠂慰位蠀蟿
  • @alexolut: That happens, yes. But in normal cases, the source is, and this is not necessary. Vanguey, as in this case, the source is. - VladD

2 answers 2

v.begin() returns an iterator, this is not a const int* .
&*v.begin() , &v[0] and v.data() are equivalent.

Using v.data() preferable because it conveys better intent.

  • four
    In addition to the intention, it is also the only function (of the proposed) that can be performed on an empty vector - ixSci
  • The iterator type is implementation-defined , so formally it can be a regular pointer. - 伪位蔚蠂慰位蠀蟿
  • @ixSci is curious that for std::basic_string &v[0] you can also call on an empty container. The main thing is not to write there. - 伪位蔚蠂慰位蠀蟿
  • one
    @alexolut, this is more like an oversight in the standard, and besides, I have a question that the description of begin() says that it returns an iterator to the first element, but there is no first element in the empty string. It seems to me, although it is not explicitly written, such behavior is not allowed, although it will work almost everywhere, at the expense of SSO. - ixSci
  • one
    @ixSci surprisingly quickly someone asked the appropriate question. Here is my answer with a footnote to the Standard as well. - 伪位蔚蠂慰位蠀蟿

No, they are not equivalent, since v.begin() returns an iterator (ie, std::vector<int>::iterator ) to the first element of the vector<int> , and &v[0] pointer to the address in memory where the element from the vector is located ( Ie int* ).

Accordingly, the interface for working with such types is different, but, again, no one forbids dereferencing an iterator (but first you should check whether the iterator points to v.end() and then take the address of the received element.

And since vector emulates the operation of a standard C array (for example, fast random access to elements), all elements in it are placed in a general ensemble (that is, they are located in a row in memory), so the transfer method &v[0] will suit you, and moving on array elements through operator ++ applied to a function parameter, for example.


But in this case, you should think in advance exactly how you will take into account the array boundaries:

  1. Pass the size of the array to the second parameter
  2. Use any barrier element that should be at the end of your array and should never be present in your array except as a barrier element, directly, and with which you will need to compare the value of the current element at each step to determine the end of the array

An example implementation through the 1st paragraph:

 #include <vector> void func(const int* parm, const int elemsCount) { for (int itemNumber = 0; itemNumber < elemsCount; ++itemNumber, ++parm) { // ToDo: 写械泄褌褋胁懈褟 褋 *parm } } int main() { std::vector<int> v = {1, 2, 3, 4, 5}; func(&v[0], v.size()); }