Reputation: 53
I have a function which utilizes std::search
to look over a certain memory region provided from start to end and find a certain byte pattern. However, under certain conditions I would like to skip over a portion of this region rather than iterate over it from start to end byte by byte. For example move 50 bytes forward from current iterator position or something similar. Is there a way extract the current iterator object and move the iterator forward using std::advance
?
const auto address = std::search( address_start, address_end, byte_pattern.cbegin(), byte_pattern.cend(),
[]( const uint8_t ¤t_byte, const memory::byte_t &pattern_byte ) {
// address of current byte in memory
// this is also the current position of the search iterator
const auto data = ¤t_byte;
if( CERTAIN CONDITION ) {
MOVE ITERATOR FORWARD SO THAT WE MOVE FORWARD IN MEMORY WE ARE SCANNING BETWEEN [address_start, address_end]
}
return pattern_byte.compare( current_byte );
}
);
Upvotes: 3
Views: 67
Reputation: 596397
There is no way to access the iterators used internally by standard library algorithms. If you need more control over iterations then you have to use iterators manually.
cppreference.com shows you possible implementations for standard library algorithms. Just copy/paste them into your code and tweak as needed. For instance, for std::search()
, it shows this implementation:
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
constexpr ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,
BinaryPredicate p)
{
for (; ; ++first) {
ForwardIt1 it = first;
for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) {
if (s_it == s_last) {
return first;
}
if (it == last) {
return last;
}
if (!p(*it, *s_it)) {
break;
}
}
}
}
Change ++s_it
as needed.
Upvotes: 4