编程语言
首页 > 编程语言> > c++响应式编程 rxcpp之cep样例解析

c++响应式编程 rxcpp之cep样例解析

作者:互联网

 1 #include "rxcpp/rx.hpp"
 2 // create alias' to simplify code
 3 // these are owned by the user so that
 4 // conflicts can be managed by the user.
 5 //创建别名以简化代码
 6 //这一步由用户处理   用户可以处理因此带来的冲突
 7 namespace rx=rxcpp;
 8 namespace rxsub=rxcpp::subjects;
 9 namespace rxu=rxcpp::util;
10 
11 #include <cctype>
12 #include <clocale>
13 
14 // At this time, RxCpp will fail to compile if the contents
15 // of the std namespace are merged into the global namespace
16 // DO NOT USE: 'using namespace std;'
17 // 不要用'using namespace std;',反之,Rxcpp将编译失败
18 int main()
19 {
20     auto keys = rx::observable<>::create<int>(
21         [](rx::subscriber<int> dest){
22             for (;;) {//被观察者循环检测用户输入字符
23                 int key = std::cin.get();
24                 dest.on_next(key);//通知观察者处理字符
25             }
26         }).
27         publish();//发布订阅
28 
29     auto a = keys.
30         filter([](int key){return std::tolower(key) == 'a';});//过滤器 过滤字符'a'
31 
32     auto g = keys.
33         filter([](int key){return std::tolower(key) == 'g';});//过滤器 过滤字符'b'
34 
35     a.merge(g).//过滤器合并
36         subscribe([](int key){//观察者订阅处理
37             std::cout << key << std::endl;
38         });
39 
40     // run the loop in create
41     keys.connect();
42 
43     return 0;
44 }

 

标签:std,rx,int,样例,namespace,c++,rxcpp,key
来源: https://www.cnblogs.com/lvdongjie/p/16319950.html