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

I have a vector defined as vector<char>, and I have some function that is 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 std::findto search for a given character - it's too slow (I know this because I profiled - :)). What I would like to do (at some point in the future, use the built-in functions of SSE4.2 to search for a character), but right now, what I want to do is vector search, that is, something like the following (not safe Job).

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

so my question is the only way to do this is to pass a vector as well and then do distanceit then &vect[index]to access the base pointer?

+3
source share
2 answers

If you know what you have std::vector<T>::iterator, then it's safe to do &*it.

Please note, however, that converting this value to is unsigned long long *unsafe, as the alignment requirements will be different.

+4
source

You can get a pointer to the beginning of your vector using &(*start). From there you can use pointer arithmetic. But what is this unsigned long long*?

+2
source

Source: https://habr.com/ru/post/1788146/


All Articles