【发布时间】:2019-07-18 08:19:03
【问题描述】:
我正在开发自己的 for_each 类型函数,通过某个整数 N。
这是我的函数目前的样子:
template<typename Container, typename Function>
void for_each_by_n( Container&& cont, Function f, unsigned increment_by ) {
using std::begin;
auto it = begin(cont);
using std::end;
auto end_it = end(cont);
while ( it != end_it ) { // traverse the full container
f(*it); // call the function pointer - object etc.
for ( unsigned n = 0; n < increment_by; ++n ) {
// here I want to increment the pointer as long as the next iteration of
// increment_by is within the bounds of the range of the container
if ( .... tried many things .... ) return; // and or break;
++it;
}
}
}
我最后一次尝试内部 if 语句是这样的:
if ( n % increment_by > (cont.size() - n) ) return; // and or break;
但是,我不断收到调试断言失败,我无法遍历容器索引的末尾。这让我很困惑,我不知道如何防止迭代结束。
【问题讨论】:
标签: debugging iterator containers c++17 assertion