c++ 之bind1st 、 bind2nd、copy_if、remove_if、remove
bind1st 和 bind2nd的用法? ?std::bind1st 和 std::bind2nd将二元函数转换为一元函数,具体用法参加下面的代码。 代码介绍了两种使用方式,第一种是使用std::less和std::greater,第二种是使用自定义的仿函数。 #include <iostream> #include <vector> #include <string> #include <iterator> #include <algorithm> #include <functional> /** * std::bind1st std::bind2nd 就是将一个二元函数的一个参数设置为定值,这样二元函数就转换为了一元函数 * 因为有些算法的参数要求必须是一元函数,但是我们又想用二元函数,那么就可以使用这两个函数 */ * *@brief std::less 仿函数的内部实现 template <class T> struct less : binary_function <T,T,bool> { bool operator() (const T& x,const T& y) const {return x<y;} }; */ struct person{ int age; std::string name; }; struct person_filter_func: public std::binary_function<person,std::string,bool> { bool operator()(const person& p,1)">const std::string& key) const{ return (p.name.find(key) != std::::npos); } }; void disp(int val){ std::cout<<val<<std::endl; } void disp_v(const person& p){ std::cout<<p.age<<","<<p.name<<std::endl; } main() { //使用 std::less 仿函数 int arr[] = {1,2,1)">3,1)">4,1)">5,1)">6,1)">7,1)">8,1)">9}; std::vector<int> vec; std::copy_if(std::begin(arr),std::end(arr),std::back_inserter(vec),std::bind1st(std::less<int>(),1)">6)); 将6 绑定为第一个参数,即 6 < value std::for_each(vec.begin(),vec.end(),disp); 7 8 9 vec.clear(); std::copy_if(std::begin(arr),std::bind2nd(std::less<将6 绑定为第二个参数,即 value < 6 std::for_each(vec.begin(),disp); 1 2 3 4 5 使用自定义的仿函数 std::vector<person> vecP; person p1 = {jack"}; vecP.push_back(p1); person p2 = {rose}; vecP.push_back(p2); person p3 = {jane}; vecP.push_back(p3); std::vector<person> vecRet; std::copy_if(vecP.begin(),vecP.end(),std::back_inserter(vecRet),std::bind2nd(person_filter_func(),1)">ja")); 将包含关键字"ja"的person,复制到vecRet容器中 std::for_each(vecRet.begin(),vecRet.end(),disp_v);1,jack 3,jane } ? copy_if:
??该函数不得修改其任何参数。这可以是函数指针或函数对象。范围不得重叠。 template <class InputIterator,1)">class OutputIterator,1)">class UnaryPredicate> OutputIterator copy_if (InputIterator first,InputIterator last,OutputIterator result,UnaryPredicate pred) { while (first!=last) { if (pred(*first)) { *result = *first; ++result; } ++first; } return result; } ? 具体使用: copy_if example #include <iostream> std::cout #include <algorithm> std::copy_if,std::distance #include <vector> std::vector main () { std::vector<int> foo = {25,1)">15,-15}; std::vector< bar (foo.size()); copy only positive numbers: auto it = std::copy_if (foo.begin(),foo.end(),bar.begin(),[](int i){return !(i<0);} ); bar.resize(std::distance(bar.begin(),it)); shrink container to new size std::cout << bar contains:; for (int& x: bar) std::cout << ' ' << x; std::cout << 'n'; return ; } ? 运行结果: std::bind1st:template <class Operation,1)">class T> binder1st<Operation> bind1st (const Operation& op,1)">const T& x) { return binder1st<Operation>(op,typename Operation::first_argument_type(x)); } std::binder1st:template <class Operation> class binder1st
: public unary_function <typename Operation::second_argument_type,typename Operation::result_type>
{
protected:
Operation op;
typename Operation::first_argument_type value;
public:
binder1st ( const Operation& x,const typename Operation::first_argument_type& y) : op (x),value(y) {}
typename Operation::result_type
operator() (const typename Operation::second_argument_type& x)
{ op(value,x); }
};
? std::remove_if:? ?remove_if的参数是迭代器,前两个参数表示迭代的起始位置和这个起始位置所对应的停止位置。 最后一个参数:传入一个回调函数,如果回调函数返回为真,则将当前所指向的参数移到尾部。 返回值是被移动区域的首个元素。 remove_if在头文件algorithm中,故要使用此函数,需添加#include <algorithm> 由于remove_if函数的参数是迭代器,通过迭代器无法得到容器本身,而要删除容器内的元素必须通过容器的成员函数来进行。 因而此函数无法真正删除元素,只能把要删除的元素移到容器末尾并返回要被删除元素的迭代器,然后通过erase成员函数来真正删除。因为一般remove_if和erase函数是成对出现的。 C++98 版本 template <class ForwardIterator,1)"> ForwardIterator remove_if (ForwardIterator first,ForwardIterator last,UnaryPredicate pred) { ForwardIterator result = first; last) { if (!pred(*first)) { *result = *first; ++result; } ++first; } result; } ? ? // C++11 版本
? 具体使用 接下来看看具体怎么用,我们将? #include <iostream>
#include <algorithm>
bool isEven(int n) 是否是偶数
{
return n % 2 == ;
}
main()
{
std::vector< vecTest;
int i = 0; i < 10; ++i)
vecTest.push_back(i);
0; i < vecTest.size(); ++i)
std::cout << vecTest[i] << " ;
std::cout << std::endl;
移动元素
std::vector<int>::iterator itor = std::remove_if(vecTest.begin(),vecTest.end(),isEven);
查看移动后的变化
删除元素
vecTest.erase(itor,vecTest.end());
;
;
}
? 运行结果为: 0 1 2 3 4 5 6 7 8 9
9 9
? 从结果可以看出,第二步调用 std::remove_if() 函数之后,vector 中的元素个数并没有减少,只是将后面不需要删除的元素移动到了 vector 的前面,从第二行结果来看,调用 std::remove_if() 函数之后返回的结果 itor 指向5,所以擦除从5所在位置到结尾的元素就达到了我们的目的。 ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |