Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
高阶函数&xAB;过滤器&xBB;在C++; 我想用C++编写一个高阶函数过滤器< /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(), filtered.end(), ostream_iterator<int const>(cout, " ")); } #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 布尔isOdd(内部常数i){ 返回i%2!=0; } 模板< 模板类容器, 类谓词, 类分配器, 甲级 > 容器过滤器(容器常量和容器、谓词常量和pred){ 容器过滤(容器); container.erase(如果(filtered.begin()、filtered.end()、pred)、filtered.end()删除_); 返回过滤; } int main(){ int const a[]={23,12,78,21,97,64}; 向量常数v(a,a+6); 向量常数过滤=过滤器(v,isOdd); 复制(filtered.begin()、filtered.end()、ostream_迭代器(cout,“”); }_C++_Templates_Stl_Higher Order Functions - Fatal编程技术网 容器过滤器(容器常量和容器、谓词常量和pred){ 容器过滤(容器); container.erase(如果(filtered.begin()、filtered.end()、pred)、filtered.end()删除_); 返回过滤; } int main(){ int const a[]={23,12,78,21,97,64}; 向量常数v(a,a+6); 向量常数过滤=过滤器(v,isOdd); 复制(filtered.begin()、filtered.end()、ostream_迭代器(cout,“”); },c++,templates,stl,higher-order-functions,C++,Templates,Stl,Higher Order Functions" /> 容器过滤器(容器常量和容器、谓词常量和pred){ 容器过滤(容器); container.erase(如果(filtered.begin()、filtered.end()、pred)、filtered.end()删除_); 返回过滤; } int main(){ int const a[]={23,12,78,21,97,64}; 向量常数v(a,a+6); 向量常数过滤=过滤器(v,isOdd); 复制(filtered.begin()、filtered.end()、ostream_迭代器(cout,“”); },c++,templates,stl,higher-order-functions,C++,Templates,Stl,Higher Order Functions" />

高阶函数&xAB;过滤器&xBB;在C++; 我想用C++编写一个高阶函数过滤器< /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(), filtered.end(), ostream_iterator<int const>(cout, " ")); } #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 布尔isOdd(内部常数i){ 返回i%2!=0; } 模板< 模板类容器, 类谓词, 类分配器, 甲级 > 容器过滤器(容器常量和容器、谓词常量和pred){ 容器过滤(容器); container.erase(如果(filtered.begin()、filtered.end()、pred)、filtered.end()删除_); 返回过滤; } int main(){ int const a[]={23,12,78,21,97,64}; 向量常数v(a,a+6); 向量常数过滤=过滤器(v,isOdd); 复制(filtered.begin()、filtered.end()、ostream_迭代器(cout,“”); }

高阶函数&xAB;过滤器&xBB;在C++; 我想用C++编写一个高阶函数过滤器< /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(), filtered.end(), ostream_iterator<int const>(cout, " ")); } #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 布尔isOdd(内部常数i){ 返回i%2!=0; } 模板< 模板类容器, 类谓词, 类分配器, 甲级 > 容器过滤器(容器常量和容器、谓词常量和pred){ 容器过滤(容器); container.erase(如果(filtered.begin()、filtered.end()、pred)、filtered.end()删除_); 返回过滤; } int main(){ int const a[]={23,12,78,21,97,64}; 向量常数v(a,a+6); 向量常数过滤=过滤器(v,isOdd); 复制(filtered.begin()、filtered.end()、ostream_迭代器(cout,“”); },c++,templates,stl,higher-order-functions,C++,Templates,Stl,Higher Order Functions,然而,在编译这段代码时,我得到了以下我无法理解的错误消息,并因此消除了这些错误消息: /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

然而,在编译这段代码时,我得到了以下我无法理解的错误消息,并因此消除了这些错误消息:

/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 here
Filter.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>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_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, 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 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, _OI) [with bool _IsMove = false, _II = __gnu_cxx::__normal_iterator<const int*, std::vector<const int, std::allocator<const int> > >, _OI = __gnu_cxx::__normal_iterator<const int*, std::vector<const int, std::allocator<const int> > >]’
/usr/include/c++/4.3/bits/stl_algobase.h:466:   instantiated from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const int*, std::vector<const int, std::allocator<const int> > >, _OI = __gnu_cxx::__normal_iterator<const int*, std::vector<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>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = const int, _Alloc = std::allocator<const int>]’
Filter.cpp:23:   instantiated from ‘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 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*&)’
/usr/include/c++/4.3/ext/new\u allocator.h:在“\u gnu\u cxx::new\u allocator”的实例化中:
/usr/include/c++/4.3/bits/allocator.h:84:从“std::allocator”实例化
/usr/include/c++/4.3/bits/stl_vector.h:75:从“std::_vector_base”实例化
/usr/include/c++/4.3/bits/stl_vector.h:176:从“std::vector”实例化
Filter.cpp:29:从此处实例化
/usr/include/c++/4.3/ext/new_allocator.h:82:错误:“const\u Tp*\uu\u gnu\u cxx::new_allocator::address(const\u Tp&)const[with _Tp=const int]”无法重载
/usr/include/c++/4.3/ext/new_allocator.h:79:错误:带有“\u-Tp*\u-gnu\u-cxx::new_allocator::address(\u-Tp&)const[带有\u-Tp=const-int]”
Filter.cpp:在函数“Container Filter(const Container&,const Predicate&)[with Container=std::vector,Predicate=bool()(int),Allocator=std::Allocator,A=const int]”中:
Filter.cpp:30:从此处实例化
Filter.cpp:23:错误:将“const std::vector”作为“\uu gnu\u cxx::\uuu normal\u迭代器std::vector::erase”(\uu gnu\u cxx::\uu normal\u迭代器,\uu gnu cxx::\uu normal\u迭代器)[with\u Tp const int,\u Alloc=std::分配器]”的“此”参数传递将丢弃限定符
/usr/include/c++/4.3/bits/stl_algo.h:在函数“\u FIter std::remove_if(\u FIter,\u FIter,\u Predicate,\u Predicate)[使用_FIter=\u gnu\u cxx::\u normal\u迭代器,\u Predicate=bool(*)(int)]中:
Filter.cpp:23:从“容器过滤器(常量容器和常量谓词)实例化[容器=std::vector,谓词=bool()(int),分配器=std::分配器,A=const int]”
Filter.cpp:30:从此处实例化
/usr/include/c++/4.3/bits/stl_-algo.h:821:错误:分配只读位置“\uu结果.\uuu-gnu\u-cxx::\uuu-normal\u迭代器::运算符*[带u-iterator=const int*,\u-Container=std::vector]()”
/usr/include/c++/4.3/ext/new_allocator.h:在成员函数“void\u gnu\u cxx::new_allocator::deallocate(_Tp*,size\u t)[带_Tp=const int]”:
/usr/include/c++/4.3/bits/stl_vector.h:150:从“void std::_vector_base:::_M_deallocate(_Tp*,size_t)[带_Tp=const int,_Alloc=std::allocator]”实例化
/usr/include/c++/4.3/bits/stl_vector.h:136:从“std::_vector_base::~_vector_base()[with _Tp=const int,_Alloc=std::allocator]实例化”
/usr/include/c++/4.3/bits/stl_vector.h:286:从“std::vector::vector(_inputierator,_inputierator,const)Alloc&)[带_inputierator=const int*,_Tp=const int,_Alloc=std::alloctor]”实例化
Filter.cpp:29:从此处实例化
/usr/include/c++/4.3/ext/new_allocator.h:98:错误:从“const void*”到“void*”的转换无效
/usr/include/c++/4.3/ext/new_allocator.h:98:错误:初始化“void operator delete(void*)”的参数1
/usr/include/c++/4.3/bits/stl_algobase.h:在函数“_oistd::__copy_move_a(_II,_II,_OI)[其中bool_IsMove=false,_II=const int*,_OI=const int*]”中:
/usr/include/c++/4.3/bits/stl_algobase.h:435:实例化自“_oistd::__copy_move_a2”(_II,_II,_OI)[带bool _IsMove=false,_II=_gnucxx:_normal_迭代器,_OI=_gnucxx:_normal迭代器]”
/usr/include/c++/4.3/bits/stl_algobase.h:466:实例化自“_oistd::copy(_II,_II,_OI)[带_II=_gnucxx::_normal_迭代器,_OI=_gnucxx:_normal_迭代器]”
/usr/include/c++/4.3/bits/vector.tcc:136:实例化自“uu gnu cxx::u normal_迭代器std::vector::erase”(uu gnu cxx::u normal_迭代器,uu gnu cxx::u normal_u迭代器)[带Tp=const int,u Alloc=std::分配器]”
Filter.cpp:23:从“容器过滤器(常量容器和常量谓词)实例化[容器=std::vector,谓词=bool()(int),分配器=std::分配器,A=const int]”
Filter.cpp:30:从此处实例化
/usr/include/c++/4.3/bits/stl_algobase.h:396:错误:调用“std::_copy_move::_copy_m(const int*&,const int*&,const int*&)”时没有匹配函数
请告诉我我做错了什么,以及实现我想要的高阶多态性的正确方法是什么

谢谢

编辑:

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

#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
布尔isOdd(内部常数i){
返回i%2!=0;
}
模板<
模板类容器,
类型名谓词,
类型名分配器,
类型名A
>
容器过滤器(容器常量和容器、谓词常量和pred){
容器过滤(容器);
filtered.erase(如果(filtered.begin()、filtered.end()、pred)、filtered.end())删除_);
返回过滤;
}
int main(){
INTA[]={23,12,78,21,97,64};
向量v(a,a+6);
向量过滤=过滤器(v,isOdd);
复制(filtered.begin()、filtered.end()、ostream_迭代器(cout,“”);
}

为什么您的
容器要参数化

template <typename C, typename P>
C filter(C const & container, P pred) {
  C filtered(container);
  filtered.erase(remove_if(filtered.begin(), filtered.end(), pred), filtered.end());
  return filtered;
}
模板
C过滤器(C容器和容器,P pred){
C过滤(容器);
过滤。擦除(如果(fil)则删除
template <typename C, typename P>
C filter(C const & container, P pred) {
  C filtered(container);
  filtered.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);
}
int main() {
  int const a[] = {23, 12, 78, 21, 97, 64};
  vector<int> const v(a, a + 6);
}
filtered.erase(remove_if(filtered.begin(), filtered.end(), pred), filtered.end());