c++响应式编程 rxcpp之cep样例解析
#include "rxcpp/rx.hpp" #include <thread> // create alias' to simplify code // these are owned by the user so that // conflicts can be managed by the user. //创建别名以简化代码 //这一步由用户处理 用户可以处理因此带来的冲突 namespace rx = rxcpp; namespace rxsub = rxcpp::subjects; namespace rxu = rxcpp::util; #include <cctype> #include <clocale> // At this time, RxCpp will fail to compile if the contents // of the std namespace are merged into the global namespace // DO NOT USE: 'using namespace std;' // 不要用'using namespace std;',反之,Rxcpp将编译失败 int main() { auto keys = rx::observable<>::create<int>(//创建一个简单的可观察数字集合 [](rx::subscriber<int> dest) { for (;;) {//被观察者循环检测用户输入字符 std::cout << "threadid observable: " << std::this_thread::get_id() << std::endl; int key = std::cin.get(); dest.on_next(key);//通知观察者处理字符 } }). publish();//发布订阅 auto a = keys. filter([](int key) {return std::tolower(key) == 'a'; });//过滤器 过滤字符'a' auto g = keys. filter([](int key) {return std::tolower(key) == 'g'; });//过滤器 过滤字符'b' auto observer = a.merge(g).//过滤器合并 subscribe([](int key) {//观察者使用 Observable 类的 Subscribe 方法订阅可观察集合 std::cout << "threadid observer: " << std::this_thread::get_id() << std::endl; std::cout << key << std::endl; }); //observer.unsubscribe(); //可取消订阅 // run the loop in create //开启循环 keys.connect(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2016-05-28 SQL2005自动备份,定期删除的维护计划及自动定期清除日志
2016-05-28 SQL Server中存储过程与函数的区别
2016-05-28 SQL Server 存储过程 .
2014-05-28 Qt那点事儿(三) 论父对象与子对象的关系
2014-05-28 Qt多线程应用--QRunnable
2014-05-28 Qt学习之如何启动和终止一个线程
2014-05-28 Qt 互斥量 QMutex