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

huangapple go评论110阅读模式
英文:

std::search extracting current iterator and moving iterator forward using std::advance

问题

我有一个函数,它利用std::search来搜索从开始到结束提供的某个内存区域,并找到特定的字节模式。然而,在某些条件下,我想跳过这个区域的一部分,而不是逐字节地从开始到结束迭代它。例如,从当前迭代器位置向前移动50个字节或类似的操作。是否有一种方法可以提取当前迭代器对象并使用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) { 
        // 内存中当前字节的地址
        // 这也是搜索迭代器的当前位置
        const auto data = &current_byte;

        if (某些条件) {
            // 在这里将迭代器向前移动,以便在扫描 [address_start, address_end] 的内存中向前移动
            // 使用 std::advance 或其他方法
        }

        return pattern_byte.compare(current_byte);
    }
);
英文:

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 );
    }
);

答案1

得分: 4

没有办法访问标准库算法内部使用的迭代器。如果需要更多对迭代的控制,那么必须手动使用迭代器。

cppreference.com 展示了标准库算法的可能实现。只需将它们复制/粘贴到您的代码中,并根据需要进行调整。例如,对于 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;
			}
		}
	}
}

根据需要更改 ++s_it

英文:

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&lt;class ForwardIt1, class ForwardIt2, class BinaryPredicate&gt;
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.

huangapple
  • 本文由 发表于 2020年1月3日 13:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定