Numeric Range Algorithms for C++20

栏目: IT技术 · 发布时间: 5年前

内容简介:TL;DR: I’ve written C++20 range-based versions of the algorithms in theC++20 brings with it updated versions of the many, many algorithms in theThe reason for this is simply one of time. It turns out that correctly defining the concepts for the numeric alg

TL;DR: I’ve written C++20 range-based versions of the algorithms in the <numeric> header, and you can get them here

C++20 brings with it updated versions of the many, many algorithms in the <algorithm> header. Sadly, as Peter Dimov recently noted , it does not do so for the “other” algorithms in the <numeric> header.

The reason for this is simply one of time. It turns out that correctly defining the concepts for the numeric algorithms is very tricky to do, and so to avoid ranges “missing the bus” with C++20 these needed to go on the back-burner for a little while. But Christopher Di Bella is currently working on it , so I’m very hopeful we’ll see conceptified, range-based versions of accumulate() and friends in C++23.

As a stop-gap solution in the mean time, I’ve written numeric_ranges.hpp – modern implementations of the most common <numeric> algorithms which you can use with C++20 ranges. You can get it here .

These implementations simply ignore the hardest part of the problem – actually defining the numeric concepts – and so, unlike the other algorithms in the std::ranges namespace, they are unconstrained and don’t offer any protection from ugly, C++98-style template error messages when you get things wrong.

Nonetheless, they do still offer the other benefits of the std::ranges algorithms, for example:

  • They have range-based overloads (of course)
  • They accept optional projections
  • They’re constexpr -callable
  • They’re CPOs , so you can (for example) pass them to other algorithms without needing to wrap them in lambdas.
constexpr std::array arr{1, 2, 3, 4};
std::vector<int> out;

tcb::partial_sum(arr, std::back_inserter(out));
// out contains [1, 3, 6, 10]

const int prod = tcb::inner_product(arr, out, 0);
// prod = (1 * 1) + (2 * 3) + (3 * 6) + (4 * 10) = 65
assert(prod == 65);

constexpr auto sq = [](int i) { return i * i; };
constexpr int sum = tcb::accumulate(arr, 0, {}, sq);
// sum = 1 + 4 + 9 + 16 = 30
static_assert(sum == 30);

Try it out!

Not using C++20 yet?

At the time of writing the only standard library implementation of ranges comes with the brand-new GCC 10.1. Other implementations are sure to follow in due time, and when they do, numeric_ranges.hpp will work with them.

But if you’re not using C++20 or a brand new GCC yet, don’t despair! You can still use all the new ranges goodness in C++17 with NanoRange – and, of course, numeric_ranges.hpp works with NanoRange too.

Enjoy!

1 : Or rather, constrained just enough to avoid ambiguous calls, and no more.

2 : Before anyone asks, Range-V3 comes with its own implementations of these algorithms, so numeric_ranges.hpp isn’t needed there.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法分析

数据结构与算法分析

(美)(C.A.谢弗)Clifford A.Shaffer / 电子工业出版社 / 1998-8 / 35.00元

本书综合“数据结构与算法”的知识梳理、习题解答及上机辅导等于一身;精心挑选了覆盖教学大纲的五百多道题目,并且提供所有题目的参考答案;对于较难的算法和上机题,给出了详细的分析和说明;对于学习的重点和难点、易犯的错误、题目的难易和重要性,以及国内教材的差异等都给出了必要的说明。 本书可给使用各种教材讲授和学习“数据结构与算法”(或者“数据结构”)的师生参考,是系统复习该课程和准备应考计算......一起来看看 《数据结构与算法分析》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具