C++之STL(十)

devtools/2024/10/21 9:40:47/

1、适配器

2、函数适配器

#include <iostream>
using namespace std;#include <algorithm>
#include <vector>
#include <functional>bool isOdd(int n)
{return n % 2 == 1;
}
int main() {int a[] = {1, 2, 3, 4, 5};vector <int> v(a, a + 5);cout << count_if(v.begin(), v.end(), isOdd) << endl;// binder2nd将二元函数对象modulus转换为医院函数对象。// binder2nd(op, value)(param)相当于op(param, value)cout << count_if(v.begin(), v.end(), binder2nd(modulus<int>(), 2)) << endl;// binder1st(op, value)(param)相当于op(value, param)cout << count_if(v.begin(), v.end(), binder1st(less<int>(), 4)) << endl;return 0;
}// 输出
3
3
1

3、针对成员函数的函数适配器

#include <iostream>
using namespace std;#include <algorithm>
#include <vector>
#include <functional>
#include <string>class Person
{
public:Person(const string& name) : name_(name){}void print() const{cout << name_ << endl;}void printWithPrefix(string prefix) const{cout << prefix << name_ << endl;}private:string name_;
};void foo(const vector<Person>& v)
{for_each(v.begin(), v.end(), mem_fun_ref(&Person::print));for_each(v.begin(), v.end(), binder2nd(mem_fun_ref(&Person::printWithPrefix), "Person: "));
}void foo2(const vector<Person*>& v)
{for_each(v.begin(), v.end(), mem_fun(&Person::print));for_each(v.begin(), v.end(), binder2nd(mem_fun(&Person::printWithPrefix), "Person: "));
}
int main() {vector<Person> v;v.push_back(Person("tom"));v.push_back(Person("jer"));foo(v);vector<Person*> v2;v2.push_back(new Person("tom"));v2.push_back(new Person("jer"));foo2(v2);return 0;
}// 输出
tom
jer
Person: tom
Person: jer
tom
jer
Person: tom
Person: jer

4、针对一般函数的函数适配器

#include <iostream>
using namespace std;#include <algorithm>
#include <vector>
#include <functional>
#include <string>
#include <cstring>int main() {char* a[] = {"", "BBB", "CCC"};vector<char*> v(a, a + 2);vector<char*>::iterator it;it = find_if(v.begin(), v.end(), binder2nd(ptr_fun(strcmp), ""));if (it != v.end()){cout << *it << endl;}return 0;
}
// 输出
BBB

http://www.ppmy.cn/devtools/56981.html

相关文章

SQL小白超详细入门教程

SQL入门教程 一、SQL概述 SQL&#xff08;Structured Query Language&#xff09;是一种用于操作关系数据库&#xff08;如MySQL、Oracle、SQL Server等&#xff09;的编程语言。它是一门ANSI&#xff08;美国国家标准化组织&#xff09;的标准计算机语言&#xff0c;用于访问…

简明万年历编制(C语言)

简明万年历编制&#xff08;C语言 &#xff09; 编制万年历的要素&#xff1a; 农历公历对照&#xff0c;显示星期&#xff0c;农历干支年&#xff0c;当年生肖&#xff0c;国定节假日&#xff0c;寒天九九&#xff0c;暑日三伏&#xff0c;入梅出梅&#xff0c;节气时间&#…

机器学习原理和代码实现专辑

1. 往期文章推荐 1.【机器学习】图神经网络(NRI)模型原理和运动轨迹预测代码实现 2. 【机器学习】基于Gumbel-Sinkhorn网络的“潜在排列问题”求解 3. 【机器学习】基于Gumbel Top-k松弛技术的图形采样 4. 【机器学习】基于Softmax松弛技术的离散数据采样 5. 【机器学习】正则…

期末考试题-通过HTML编程Vue3选项式:简易购物车

<!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><!-- 引用 element-plus 样式 --><!-- 注意&#xff1a;复…

Kafka 和 RabbitMQ对比

Kafka和RabbitMQ是两种广泛使用的消息队列系统&#xff0c;它们在设计理念、架构和功能上有很多相似之处&#xff0c;但也有许多显著的区别。以下是两者之间的异同点&#xff0c;以表格的形式详细阐述&#xff1a; 特性KafkaRabbitMQ消息模型基于日志&#xff08;Log-based&am…

VSCode + GDB + J-Link 单片机程序调试实践

VSCode GDB J-Link 单片机程序调试实践 本文介绍如何创建VSCode的调试配置&#xff0c;如何控制调试过程&#xff0c;如何查看修改各种变量。 安装调试插件 在 VSCode 扩展窗口搜索安装 Cortex-Debug插件 创建调试配置 在 Run and Debug 窗口点击 create a launch.json …

【Qt】之【Bug】大量出现“未定义的标识符”问题

背景 构建时出现大量错误 原因 中文注释问题 解决 方法1. 报错代码附近的中文注释全部删掉。。。 方法2. 报错的文件添加 // Chinese word comment solution #pragma execution_character_set("utf-8")

c++笔记

位运算符优先级 乘除求余>加减>移位运算符(>>, <<)>比较运算>位运算>逻辑运算符 位运算优先级没有比较运算符优先级高。 指针与引用写法 int& a, b; 定义了一个int引用a和int b。至于int& a还是int &a都可以&#xff0c;是不同的风格…