C++ //练习 14.42 使用标准库函数对象及适配器定义一条表达式,令其(a)统计大于1024的值有多少个。(b)找到第一个不等于pooh的字符串。(c)将所有的值乘以2。

C++ Primer(第5版) 练习 14.42

练习 14.42 使用标准库函数对象及适配器定义一条表达式,令其

( a ) 统计大于1024的值有多少个。

( b ) 找到第一个不等于pooh的字符串。

( c ) 将所有的值乘以2。

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
/*************************************************************************
	> File Name: ex14.42.cpp
	> Author: 
	> Mail: 
	> Created Time: Tue 09 Jul 2024 08:07:42 AM CST
 ************************************************************************/

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;

int main(){
    vector<int> num = {1, 2304, 323, 7896, 99, 2401};
    vector<string> str = {"pooh", "pooh", "hello", "pooh", "world"};

    auto count = count_if(num.begin(), num.end(), bind(greater<int>(), placeholders::_1, 1024));
    cout<<">1024 = "<<count<<endl;


    vector<string>::iterator iter = find_if_not(str.begin(), str.end(), bind(equal_to<string>(), placeholders::_1, "pooh" ));
    if(iter != str.end()){
        cout<<*iter<<endl;
    }

    transform(num.begin(), num.end(), num.begin(), bind(multiplies<int>(), placeholders::_1, 2));
    for(auto n : num){
        cout<<n<<" ";
    }
    cout<<endl;
    
    return 0;
}
运行结果显示如下:

在这里插入图片描述

  • 15
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值