将函数对象用于标准库算法


标准库函数对象,eg.equal_to<Type>

适配器eg. bind2nd(less_equal<int>(), 10)

not1(bind2nd(less_equal<int>(),10))


//函数对象的函数适配器
#include"head.h"

int main(){
	//绑定器binder:bind1st,bind2nd;绑定一个现成的实参到二元函数对象函数第一个或第二个参数,使之变成一元函数
	int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	std::vector<int> vec(a, a + 10); 
	std::cout << count_if(vec.begin(), vec.end(), std::bind2nd(std::less_equal<int>(), 7)) << std::endl;
	//less_equal<Type> 不支持std::string类型
	
	//求反器negator:not1,not2;将一元函数对象或二元函数对象的真值求反
	//求反与绑定的嵌套使用
	std::cout << count_if(vec.begin(), vec.end(), not1(std::bind2nd(std::less_equal<int>(), 7))) << std::endl;
	
}


pe14_37.cpp,

38,39都被兼容了

(mark)  c题的意思?要求

//使用标准库函数对象和函数适配器,定义一个对象用于:(a)、(b)、(c)
//不是要一个对象完成三个功能(太恐怖了,重载?),是三个小题吧~!
#include"head.h"

int main(){
    //a)查找大于1024的所有值
    int a[] = {10000, 2, 3, 4, 5, 6, 1024, 8, 9, 1025};
    std::vector<int> vec(a, a + 10); 
    //所谓定义一个对象,greater就是--函数对象
    std::cout << count_if(vec.begin(), vec.end(), std::bind2nd(std::greater<int>(), 1024)) << std::endl;
    std::cout << count_if(vec.begin(), vec.end(), std::bind2nd(std::greater_equal<int>(), 1025)) << std::endl;
    std::cout << count_if(vec.begin(), vec.end(), std::not1(std::bind2nd(std::less_equal<int>(), 1024))) << std::endl;
    std::cout << count_if(vec.begin(), vec.end(), std::not1(std::bind2nd(std::less<int>(), 1025))) << std::endl;
    //b)查找不等于pooh的所有字符串
    //find_if()找出来打印?count_if()计算出来数量?
    std::vector<std::string> svec(3, "world");
    svec.push_back("hello");
    svec.push_back("pooh");
    std::string compare = "pooh";
    //方法一,bind2nd绑定not_equal_to,用count_if计数
    std::cout << "the number of strings not sames to " << compare << ": "
            <<count_if(svec.begin(), svec.end(), std::bind2nd(std::not_equal_to<std::string>(), compare)) 
            << std::endl;
    //方法二:用bind2nd绑定equal_to和要查找string对象"pooh",外层not1,
    //这里只能not1吧,这里是标准库算法传递一个对象进来求bool值,只能是调用一元函数对象
    std::cout << "the number of strings sames to " << compare << ": "
            << count_if(svec.begin(), svec.end(), std::not1(std::bind2nd(std::equal_to<std::string>(), compare)))
            << std::endl;//not1求反,或者用logical_not<Type>
    //c)将所有值乘以2
    //我该用什么算法遍历,用count_if过一遍,再用multiplies<Type>乘以2?No~!!!!
    //按理说都是传递参数进行指定操作啊,标准库算法只管传实参,里边用哪个操作他就别管我了,multiplies也没说不允许传递
    //错误:因为传进去即使能做操作,可能是给副本做的操作,又不能返回引用和副本来改原值。。。find_if更没戏,是寻找位置返回迭代器的
    //现在能想到的,只有需要传递一个函数的标准库算法,实际上没接触几个~!做判断用的返回bool类型的又无效,
    int multi = 2;
    std::cout << count_if(vec.begin(), vec.end(), std::bind2nd(std::multiplies<int>(), multi)) << std::endl;
    //我更相信他要的是这个意思(可能需要个重载,来搞定doubl之类的?):
    //类似于    std::plus<int> intAdd;定义一个对象,没要求非要把一个容器里边的元素乘以2,是谁调用它谁乘以2(a、b两题明显是对整个容器)
    //std::bind2nd(std::multiplies<int>, multi) intMulti2;//可是这个声明不出来
    //打印验证
    for(std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); iter++){
        std::cout << *iter << "\t";
    }
    
    
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值