简体   繁体   English

std::search 提取当前迭代器并使用 std::advance 向前移动迭代器

[英]std::search extracting current iterator and moving iterator forward using std::advance

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.我有一个函数,它利用std::search来查看std::search提供的某个内存区域并找到某个字节模式。 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.例如,从当前迭代器位置或类似位置向前移动 50 个字节。 Is there a way extract the current iterator object and move the iterator forward using std::advance ?有没有办法提取当前迭代器对象并使用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 );
    }
);

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. cppreference.com向您展示了标准库算法的可能实现。 Just copy/paste them into your code and tweak as needed.只需将它们复制/粘贴到您的代码中并根据需要进行调整。 For instance, for std::search() , it shows this implementation:例如,对于std::search() ,它显示了这个实现:

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.根据需要更改++s_it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM