Convert iterator to pointer?

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

    That seems not possible in my situation, since the function I mentioned is the find function of unordered_set.

    Are you using custom hash/predicate function objects? If not, then you must pass unordered_set*>::find() the pointer to the exact vector that you want to find. A pointer to another vector with the same contents will not work. This is not very useful for lookups, to say the least.

    Using unordered_set > would be better, because then you could perform lookups by value. I think that would also require a custom hash function object because hash does not to my knowledge have a specialization for vector.

    Either way, a pointer into the middle of a vector is not itself a vector, as others have explained. You cannot convert an iterator into a pointer to vector without copying its contents.

提交回复
热议问题