1

我应该实现一个模板函数,该函数遍历迭代器范围,检查参数谓词的条件是否满足值,并且使用参数插入迭代器将不满足谓词条件的值复制到参数输出。

我编写了一个主程序来测试我的模板函数实现,它没有返回错误,但我大学的测试程序不会与我的模板函数实现一起编译,并给出以下错误:

/usr/include/c++/4.4/debug/safe_iterator.h:272: error: no match for 'operator+=' in '((__gnu_debug::_Safe_iterator<std::__norm::_List_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__debug::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >*)this)->__gnu_debug::_Safe_iterator<std::__norm::_List_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__debug::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::_M_current += __n'¶

我的实现是:

template <typename IteratorIn, typename IteratorOut, typename Predicate>
IteratorOut copyIfNot(IteratorIn begin, IteratorIn end, IteratorOut out, Predicate pred) {
    for (IteratorIn iter = begin; iter != end; iter++) {
        if (!pred(*iter)) {
            std::copy(iter, iter + 1, out);
        }
    }

    return out;
}

你能提示我错误可能在哪里吗?

4

1 回答 1

1

显然,您正在使用您的函数 with list::iterator,它不是随机访问迭代器,并且不会operator+像您在 in 中使用的那样实现iter + 1

您必须复制并operator++在其上使用:

auto itercopy = iter;
std::copy(iter, ++itercopy, out);
于 2012-10-14T23:19:54.243 回答