Touloudou
Touloudou

Reputation: 2223

pointer from random access iterator

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

Answers (1)

lubgr
lubgr

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

Related Questions