Reputation: 2223
I have a random access iterator, and I need to turn it into a pointer (external API demands it!). I could do T* ptr = &(*iter);
, but I find it rather ugly. Is there a better way? All random access iterators usually implement the ->
operator, but I am not aware of a generic way to just get the pointer to the data.
Upvotes: 0
Views: 170
Reputation: 38267
If you can use C++20, there is std::to_address. Example:
std::vector<int> v{1, 2, 3};
int *ptr = std::to_address(v.begin());
Upvotes: 5