1
我應該實現一個覆蓋迭代器範圍的模板函數,用於檢查參數謂詞的條件是否滿足值,並且不滿足謂詞條件的值是否使用參數複製到參數輸出插入迭代器。帶迭代器的C++模板函數
我已經寫了一個主要程序來測試我的模板功能的實現,它不返回任何錯誤,但我的大學的測試程序將不符合我的模板函數執行編譯並提供了以下錯誤:
/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;
}
你能告訴我錯誤可能在哪裏嗎?
不要拷貝只有一個元素使用拷貝。更好的是:'* out = * iter; ++出來;'。爲什麼錯誤 - 良好的答案已經提供。 – PiotrNycz