Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access the underlying pointer from a given iterator to a vector?

Tags:

c++

iterator

I have a vector defined as vector<char>, and I have some function which gets called at some point that receives a range - something like the following:

template <typename Iterator>
void foo(Iterator& start, Iterator& end)
{
}

In the above function, I currently have a call to std::find to look for a given character - this is way too slow (I know this because I have profiled - :) ). What I'd like to do (at some point in the future is to use the SSE4.2 intrinsics to search for the character), but right now, what I want to do is a vector search, i.e. something like the following (not safe operation).

unsigned long long* scanv = reinterpret_cast<unsigned long long*>(<access pointer at start>);
// some byte magic.

so my question is - is the only way to do this is to also pass the vector as well and then do a distance, then &vect[index] to access the underlying pointer?

like image 239
Nim Avatar asked Dec 15 '25 08:12

Nim


1 Answers

If you know that you have a std::vector<T>::iterator, then it is safe to do &*it.

Note, however, that it is not safe to convert this to unsigned long long *, as the alignment requirements will be different.

like image 110
Oliver Charlesworth Avatar answered Dec 16 '25 22:12

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!