3

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 &current_byte, const memory::byte_t &pattern_byte ) { 
        // address of current byte in memory
        // this is also the current position of the search itterator
        const auto data = &current_byte;

        if( CERTAIN CONDITION ) {
            MOVE ITTERATOR FORWARD SO THAT WE MOVE FORWARD IN MEMORY WE ARE SCANNING BETWEEN [address_start, address_end]

        }

        return pattern_byte.compare( current_byte );
    }
);
John James
  • 53
  • 4
  • Does this answer your question? [How to increment an iterator by 2?](https://stackoverflow.com/questions/1057529/how-to-increment-an-iterator-by-2) – alter_igel Jan 03 '20 at 05:33
  • @alter igel The usage of std::advance here requires the iterator object to be passed into the function. I'm not sure how to obtain the current iterator object from my function. I edited my question to contain this. – John James Jan 03 '20 at 05:45
  • 1
    Please, have a look at [std::search()](https://en.cppreference.com/w/cpp/algorithm/search) - specifically at the [Possible implementation](https://en.cppreference.com/w/cpp/algorithm/search#Possible_implementation). AFAIS, the iterator is not passed to the predicate. Even more worse, the call of predicate may be repeated for the same elements (due to comparing ranges with ranges element by element). I believe you have to implement your own flavor for your specific needs. – Scheff's Cat Jan 03 '20 at 06:32
  • How about using a functor with a counter? You can set the functor's counter as necessary and the functor can immediately return based on the counter(skip certain iterations). Though I won't personally do this myself. – SacrificerXY Jan 03 '20 at 06:37

1 Answers1

4

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770