Convert iterator to pointer?

前端 未结 12 2309
野性不改
野性不改 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:46

    The direct answer to your question is yes. If foo is a vector, you can do this: &foo[1].

    This only works for vectors however, because the standard says that vectors implement storage by using contigious memory.

    But you still can (and probably should) pass iterators instead of raw pointers because it is more expressive. Passing iterators does not make a copy of the vector.

提交回复
热议问题