functional filter()
程序员文章站
2022-04-28 13:57:21
suibianwanwan ......
1 #include "pch.h" 2 #include <iostream> 3 #include <deque> 4 #include <string> 5 #include <list> 6 #include <algorithm> 7 #include <vector> 8 #include <functional> 9 using namespace std; 10 11 template<typename inputiterator, typename outputiterator,typename elemtype, typename comp> 12 outputiterator 13 filter(inputiterator first, inputiterator last,
outputiterator at, const elemtype &val, comp pred) 14 { 15 while((first=find_if(first, last, bind2nd(pred, val)))!=last) 16 { 17 cout << "found value:" << *first << endl; 18 *at++ = *first++; 19 } 20 return at; 21 } 22 23 int main() 24 { 25 const int elem_size = 8; 26 int ia[elem_size] = { 12,8,43,0,6,21,3,7 }; 27 vector<int> ivec(ia,ia+elem_size); 28 int ia2[elem_size]; 29 vector<int> ivec2(elem_size); 30 cout << "filtering integer array for values less than 8\n"; 31 filter(ia, ia + elem_size, ia2, elem_size, less<int>()); 32 cout << "filtering integer vector for values greater than 8\n"; 33 filter(ivec.begin(), ivec.end(), ivec2.begin(), elem_size, greater<int>()); 34 return 0; 35 }
推荐阅读
-
Js数组方法:filter()、map()、some()、every()、forEach()、indexOf()、lastIndexOf()运用实例
-
PHP过滤器 filter_has_var() 函数用法实例分析
-
过滤器模式(Filter Pattern)
-
解决cannot be cast to javax.servlet.Filter 报错的问题
-
JSP 开发中过滤器filter设置编码格式的实现方法
-
Python3基础-特别函数(map filter partial reduces sorted)实例学习
-
详解CSS3中强大的filter(滤镜)属性
-
php使用filter过滤器验证邮箱 ipv6地址 url验证
-
JSP Filter的应用方法
-
AngularJS过滤器filter用法分析