Convert iterator to pointer?

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

    A vector is a container with full ownership of it's elements. One vector cannot hold a partial view of another, even a const-view. That's the root cause here.

    If you need that, make your own container that has views with weak_ptr's to the data, or look at ranges. Pair of iterators (even pointers work well as iterators into a vector) or, even better, boost::iterator_range that work pretty seamlessly.

    It depends on the templatability of your code. Use std::pair if you need to hide the code in a cpp.

提交回复
热议问题