当前位置:   article > 正文

c++ 函数式编程-管道流水线

c++ 函数式编程-管道流水线

c++ 管道式函数式编程

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

template <typename T, class F>
auto operator|(T&& param, F&& f) -> decltype(std::forward<F>(f)(std::forward<T>(param))) {
    return (std::forward<F>(f)(std::forward<T>(param)));
}

template <class F, typename Tuple, size_t... I>
auto process(F&& f, Tuple&& tu, index_sequence<I...> rr) {
    return std::forward<F>(f)(get<I>(std::forward<Tuple>(tu))...);
}

template <typename Tuple, class F>
auto operator|(Tuple&& tu, F&& f) -> decltype(process(forward<F>(f), forward<Tuple>(tu), make_index_sequence<tuple_size<typename remove_reference<Tuple>::type>::value>())) {
    return process(forward<F>(f), forward<Tuple>(tu), make_index_sequence<tuple_size<typename remove_reference<Tuple>::type>::value>());
}

auto sorter = [](auto&& nums) -> decltype(auto) {
    sort(nums.begin(), nums.end());
    return (nums);
};

auto uniquer = [](auto&& nums) -> decltype(auto) {
    nums.erase(unique(nums.begin(), nums.end()), nums.end());
    return (nums);
};

auto printer = [](auto&& nums) {
    for (auto v : nums) {
        cout << v << " ";
    }
    cout << endl;
};

string generate_data(string filename, int low, int high, int times) {
    ofstream os(filename);
    os.clear();
    random_device rd;
    mt19937 mt(rd());
    uniform_int_distribution<int> u(low, high);
    for (int i = 0; i < times; i++) {
        os << u(mt) << endl;
    }
    return filename;
}

vector<int> read_data(string filename) {
    ifstream is(filename);
    int num;
    vector<int> vec;
    while (!is.eof()) {
        is >> num;
        vec.push_back(num);
    }
    return vec;
}

unordered_map<int, int> statis_times(vector<int>&& nums) {
    unordered_map<int, int> mp;
    for (auto v : nums) {
        mp[v]++;
    }
    return mp;
}

vector<pair<int, int>> value_key_reverse(unordered_map<int, int>&& mp) {
    vector<pair<int, int>> vec;
    for (auto& kv : mp) {
        vec.push_back({kv.second, kv.first});
    }
    return vec;
}

auto sort_greater = [](vector<pair<int, int>>&& nums) {
    sort(nums.begin(), nums.end(), greater<>{});
    return make_tuple(nums, 20);
};

void top_print(vector<pair<int, int>> data, int top) {
    for (int i = 0; i < top && i < data.size(); i++) {
        cout << data[i].second << "\t" << data[i].first << endl;
    }
}

int main() {
    // // 单参函数式编程
    vector<int>{2, 1, 3, 5, 2} | sorter | uniquer | printer;
    // 读文件, 统计每个数出现次数,并打印前10
    cout << "管道 \n";
    make_tuple("./radom.txt", 0, 10, 100) | generate_data | read_data | statis_times | value_key_reverse | sort_greater | top_print;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175

输出

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/254690
推荐阅读
相关标签
  

闽ICP备14008679号