让C++写起来和Python一样简单的库range-v3
2017-09-15 18:37
513 查看
最近看了一篇博文:C++ 17 写法上已经很接近 python 了。这里引出了一个很骚的库:range-v3。这个库可以让C++更加容易实现filter/mapreduce/sort/iterator等函数。
range-v3需要clang 3.6.2 及以上和GCC 4.9.1及以上的编译器版本支持,毕竟里面好多代码是以C++11/14/17为基础写的。
源码地址:https://github.com/ericniebler/range-v3。
一.安装:
在win和linux系统下都可以
然后把range-v3-0.3.0\include下的所有文件复制到你的GCC/Clang目录的include目录下,然后就可以使用了:
以上代码会打印出单边小于200的满足勾股定理的三角形的三条边长度。
range-v3需要clang 3.6.2 及以上和GCC 4.9.1及以上的编译器版本支持,毕竟里面好多代码是以C++11/14/17为基础写的。
源码地址:https://github.com/ericniebler/range-v3。
一.安装:
在win和linux系统下都可以
git clone https://github.com/ericniebler/range-v3.git
然后把range-v3-0.3.0\include下的所有文件复制到你的GCC/Clang目录的include目录下,然后就可以使用了:
#include <chrono> #include <iostream> #include <range/v3/all.hpp> using namespace ranges; int main() { auto triples = view::for_each(view::ints(1), [](int z) { return view::for_each(view::ints(1, z+1), [=](int x) { return view::for_each(view::ints(x, z+1), [=](int y) { return yield_if(x*x + y*y == z*z, std::make_tuple(x, y, z)); }); }); }); //// This alternate syntax also works: //auto triples = ints(1) >>= [] (int z) { return // ints(1, z+1) >>= [=](int x) { return // ints(x, z+1) >>= [=](int y) { return // yield_if(x*x + y*y == z*z, // std::make_tuple(x, y, z)); };}; }; // Display the first 100 triples RANGES_FOR(auto triple, triples | view::take(100)) { std::cout << '(' << std::get<0>(triple) << ',' << std::get<1>(triple) << ',' << std::get<2>(triple) << ')' << '\n'; } } class timer { private: std::chrono::high_resolution_clock::time_point start_; public: timer() { reset(); } void reset() { start_ = std::chrono::high_resolution_clock::now(); } std::chrono::milliseconds elapsed() const { return std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now() - start_); } friend std::ostream &operator<<(std::ostream &sout, timer const &t) { return sout << t.elapsed().count() << "ms"; } }; void benchmark() { // Define an infinite range containing all the Pythagorean triples: auto triples = view::for_each(view::ints(1), [](int z) { return view::for_each(view::ints(1, z+1), [=](int x) { return view::for_each(view::ints(x, z+1), [=](int y) { return yield_if(x*x + y*y == z*z, std::make_tuple(x, y, z)); }); }); }); static constexpr int max_triples = 3000; timer t; int result = 0; RANGES_FOR(auto triple, triples | view::take(max_triples)) { int i, j, k; std::tie(i, j, k) = triple; result += (i + j + k); } std::cout << t << '\n'; std::cout << result << '\n'; result = 0; int found = 0; t.reset(); for(int z = 1;; ++z) { for(int x = 1; x <= z; ++x) { for(int y = x; y <= z; ++y) { if(x*x + y*y == z*z) { result += (x + y + z); ++found; if(found == max_triples) goto done; } } } } done: std::cout << t << '\n'; std::cout << result << '\n'; }
以上代码会打印出单边小于200的满足勾股定理的三角形的三条边长度。
相关文章推荐
- C++调用python:无参数,简单helloworld示例
- 最简单的 C++ 中嵌入 Python 例子
- C++程序调用Python的函数(简单应用)及Ubuntu16.04下codeblocks的环境配置
- 量化交易中VWAP/TWAP算法的基本原理和简单源码实现(C++和python)(转)
- 【转帖】windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)
- 简单的Java,Python,C,C++
- python range的最简单用法
- 利用 c++模板 类型 推导思想,实现最简单的 判断两个类型 是否一样的 方法
- [C++ && Python]简单模拟代码评测系统
- VS平台简单的使用C++调用Python3.5的方法
- 在C/C++中调用python的简单笔记part 1
- 【原创】C++链表如何像Python List一样支持多种数据类型
- windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)
- windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)
- 构建简单的 C++ 服务组件,第 2 部分: 通过服务组件体系结构使用 Python、Ruby 和 Web 服务
- 转载:Python 与 C++ 程序的简单实例对比
- 简单使用scipy.weave混合使用Python和C++代码的简单例子
- 算法导论-排序的Python&C++简单实现
- tomato固件 路由简单玩 BT 电驴 amule highId python 其实dd wrt也一样 含 amule_2.2.6-1_mipsel.ipk下载
- C++ Boost 之Python(一个简单的例子)