C中的高阶函数“filter”

C中的高阶函数“filter”,第1张

概述我想用C编写一个更高阶的函数滤波器.我到目前为止提出的代码如下: #include <iostream>#include <string>#include <functional>#include <algorithm>#include <vector>#include <list>#include <iterator>using namespace std;bool isOd 我想用C编写一个更高阶的函数滤波器.我到目前为止提出的代码如下:

#include <iostream>#include <string>#include <functional>#include <algorithm>#include <vector>#include <List>#include <iterator>using namespace std;bool isOdd(int const i) {  return i % 2 != 0;}template <  template <class,class> class Container,class Predicate,class Allocator,class A>Container<A,Allocator> filter(Container<A,Allocator> const & container,Predicate const & pred) {  Container<A,Allocator> filtered(container);  container.erase(remove_if(filtered.begin(),filtered.end(),pred),filtered.end());  return filtered;}int main() {  int const a[] = {23,12,78,21,97,64};  vector<int const> const v(a,a + 6);  vector<int const> const filtered = filter(v,isOdd);  copy(filtered.begin(),ostream_iterator<int const>(cout," "));}

但是,在编译此代码时,我收到以下错误消息,我无法理解,因此摆脱:

/usr/include/c++/4.3/ext/new_allocator.h: In instantiation of ‘__gnu_cxx::new_allocator<const int>’:/usr/include/c++/4.3/bits/allocator.h:84:   instantiated from ‘std::allocator<const int>’/usr/include/c++/4.3/bits/stl_vector.h:75:   instantiated from ‘std::_Vector_base<const int,std::allocator<const int> >’/usr/include/c++/4.3/bits/stl_vector.h:176:   instantiated from ‘std::vector<const int,std::allocator<const int> >’Filter.cpp:29:   instantiated from here/usr/include/c++/4.3/ext/new_allocator.h:82: error: ‘const _TP* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp = const int]’ cannot be overloaded/usr/include/c++/4.3/ext/new_allocator.h:79: error: with ‘_TP* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const int]’Filter.cpp: In function ‘Container<A,Allocator> filter(const Container<A,Allocator>&,const Predicate&) [with Container = std::vector,Predicate = bool ()(int),Allocator = std::allocator<const int>,A = const int]’:Filter.cpp:30:   instantiated from hereFilter.cpp:23: error: passing ‘const std::vector<const int,std::allocator<const int> >’ as ‘this’ argument of ‘__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp,_Alloc>::_Tp_alloc_type::pointer,std::vector<_Tp,_Alloc> > std::vector<_Tp,_Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp,_Alloc> >,__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp,_Alloc> >) [with _Tp = const int,_Alloc = std::allocator<const int>]’ discards qualifIErs/usr/include/c++/4.3/bits/stl_algo.h: In function ‘_FIter std::remove_if(_FIter,_FIter,_Predicate) [with _FIter = __gnu_cxx::__normal_iterator<const int*,std::vector<const int,std::allocator<const int> > >,_Predicate = bool (*)(int)]’:Filter.cpp:23:   instantiated from ‘Container<A,A = const int]’Filter.cpp:30:   instantiated from here/usr/include/c++/4.3/bits/stl_algo.h:821: error: assignment of read-only location ‘__result.__gnu_cxx::__normal_iterator<_Iterator,_Container>::operator* [with _Iterator = const int*,_Container = std::vector<const int,std::allocator<const int> >]()’/usr/include/c++/4.3/ext/new_allocator.h: In member function ‘voID __gnu_cxx::new_allocator<_Tp>::deallocate(_TP*,size_t) [with _Tp = const int]’:/usr/include/c++/4.3/bits/stl_vector.h:150:   instantiated from ‘voID std::_Vector_base<_Tp,_Alloc>::_M_deallocate(_TP*,size_t) [with _Tp = const int,_Alloc = std::allocator<const int>]’/usr/include/c++/4.3/bits/stl_vector.h:136:   instantiated from ‘std::_Vector_base<_Tp,_Alloc>::~_Vector_base() [with _Tp = const int,_Alloc = std::allocator<const int>]’/usr/include/c++/4.3/bits/stl_vector.h:286:   instantiated from ‘std::vector<_Tp,_Alloc>::vector(_inputIterator,_inputIterator,const _Alloc&) [with _inputIterator = const int*,_Tp = const int,_Alloc = std::allocator<const int>]’Filter.cpp:29:   instantiated from here/usr/include/c++/4.3/ext/new_allocator.h:98: error: invalID conversion from ‘const voID*’ to ‘voID*’/usr/include/c++/4.3/ext/new_allocator.h:98: error:   initializing argument 1 of ‘voID operator delete(voID*)’/usr/include/c++/4.3/bits/stl_algobase.h: In function ‘_OI std::__copy_move_a(_II,_II,_OI) [with bool _IsMove = false,_II = const int*,_OI = const int*]’:/usr/include/c++/4.3/bits/stl_algobase.h:435:   instantiated from ‘_OI std::__copy_move_a2(_II,_II = __gnu_cxx::__normal_iterator<const int*,_OI = __gnu_cxx::__normal_iterator<const int*,std::allocator<const int> > >]’/usr/include/c++/4.3/bits/stl_algobase.h:466:   instantiated from ‘_OI std::copy(_II,_OI) [with _II = __gnu_cxx::__normal_iterator<const int*,std::allocator<const int> > >]’/usr/include/c++/4.3/bits/vector.tcc:136:   instantiated from ‘__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp,_Alloc = std::allocator<const int>]’Filter.cpp:23:   instantiated from ‘Container<A,A = const int]’Filter.cpp:30:   instantiated from here/usr/include/c++/4.3/bits/stl_algobase.h:396: error: no matching function for call to ‘std::__copy_move<false,true,std::random_access_iterator_tag>::__copy_m(const int*&,const int*&,const int*&)’

请告诉我我在这里做错了什么,以及实现我想要的那种高阶多态的正确方法是什么.

谢谢.

编辑:

谢谢大家.这是我在应用你的建议后得到的新代码(它现在可以使用了,是的!)

#include <iostream>#include <string>#include <functional>#include <algorithm>#include <vector>#include <List>#include <iterator>using namespace std;bool isOdd(int const i) {  return i % 2 != 0;}template <  template <typename,typename> class Container,typename Predicate,typename Allocator,typename A>Container<A,Allocator> filtered(container);  filtered.erase(remove_if(filtered.begin(),filtered.end());  return filtered;}int main() {  int a[] = {23,64};  vector<int> v(a,a + 6);  vector<int> filtered = filter(v,ostream_iterator<int>(cout," "));}
解决方法 为什么容器参数化了?

template <typename C,typename P>C filter(C const & container,P pred) {  C filtered(container);  filtered.erase(remove_if(filtered.begin(),filtered.end());  return filtered;}

也适用.请注意,我通过值而不是const引用传递P,正如Meyers在Effective C中所建议的那样(迭代器和仿函数应该通过值传递).

总结

以上是内存溢出为你收集整理的C中的高阶函数“filter”全部内容,希望文章能够帮你解决C中的高阶函数“filter”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1218948.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存