赞
踩
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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。