Convert iterator to pointer?

前端 未结 12 2281
野性不改
野性不改 2020-12-14 14:20

I have a std::vector with n elements. Now I need to pass a pointer to a vector that has the last n-1 elements to a function.

F

12条回答
  •  执念已碎
    2020-12-14 14:35

    If your function really takes vector * (a pointer to vector), then you should pass &foo since that will be a pointer to the vector. Obviously that will not simply solve your problem, but you cannot directly convert an iterator to a vector, since the memory at the address of the iterator will not directly address a valid vector.

    You can construct a new vector by calling the vector constructor:

    template  vector(InputIterator, InputIterator)
    

    This constructs a new vector by copying the elements between the two iterators. You would use it roughly like this:

    bar(std::vector(foo.begin()+1, foo.end());
    

提交回复
热议问题