Is it possible to access the underlying pointer from a given iterator to a vector?
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 th开发者_C百科e vector as well and then do a distance
, then &vect[index]
to access the underlying pointer?
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.
You can get a pointer to start of your vector using &(*start)
. From there on, you can use pointer arithmetic. But what's that about unsigned long long*
?
精彩评论