gpt4 book ai didi

c++ - 为什么 STL 数值算法使用 'op' 而不是 'op=' ?

转载 作者:可可西里 更新时间:2023-11-01 17:01:36 24 4
gpt4 key购买 nike

为什么 std::numeric 算法似乎更喜欢 op 而不是 op= ?例如,这里是 std::accumulate 在 LLVM 中的实现:

template <class _InputIterator, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
{
for (; __first != __last; ++__first)
__init = __init + *__first;
return __init;
}

如果使用 += 运算符实现,这会不会更高效/更简洁/更好?

最佳答案

标准中是这样定义的……

标准是根据+定义的,而不是+=:

26.7.2 Accumulate [accumulate]

template <class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init);
template <class InputIterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);

Effects: Computes its result by initializing the accumulator acc with the initial value init and then modifies it with acc = acc + *i or acc = binary_op(acc, *i) for every iterator i in the range [first,last) in order.

其他数值算法也是如此。

…标准基于STL…

但这就是为什么它们现在实现的原因。然而,要了解最初的原因,需要 go further down the rabbit hole :

Requirements on types

For the first version, the one that takes two arguments:

  • InputIterator is a model of Input Iterator.
  • T is a model of Assignable.
  • If x is an object of type T and y is an object of InputIterator's value type, then x + y is defined.
  • The return type of x + y is convertible to T.

……STL 是由……创建的?

那么,这是为什么呢?让我们问问 Alexander Stepanov,STL 背后的思想:

[A] user has asked a [question] on StackOverflow two days ago concerning the implementation and formulation of numerical algorithms like accumulate or inner_product, which are defined in terms of + instead of += (section 26.7 in ISO C++11).

I've tried to find some rationale behind the decision, but even the version on SGI's page doesn't mention anything regarding this particular choice of the operator.

Was the choice of the operator (a = a + b instead of a += b) only based on your personal preference, as some comments assume? Was a+b the more natural way to write the operation back then? Or was it simply a matter of symmetry between a = a +b and a = bin_op(a,b)?

-- [Zeta]

现在,在您阅读他的回答之前,请记住他大约 30 年前开始编写通用库,以及他和 Meng Lee 的初始文档 The Standard Template Library今年 10 月将迎来成立 20 周年。话不多说,我给出他的答案:

I suspect that it was a matter of symmetry between a = a + b and a = bin_op(a,b), but I really do not remember. I should have written a rationale document stating all the reasoning between different design choices in STL, but I did not. Sorry.

(如果 Stepanov 偶然读到了这篇文章:再次感谢您的回答!)

个人认为 Common Lisp 的 reduce 的灵感是另一个因素,但这只是推测。

从中可以学到什么?

有时,标准的决定是基于个人喜好和优雅。例如,如果 Stroustrup 编写了 STL,他会“非常喜欢[编辑]使用 a+=b 作为更直接的意图表达,并且通常比 a=a+b 更快”。但是,我不得不承认 a = a+b 和 a=bin_op(a,b) 之间的对称性有其自身的美。

话虽如此,a+=ba=a+b 的争论将促进挑战:

This issue will become very important when we define standard concepts, and I do not know how it will be resolved. [Stroustrup; also thanks to him for answering my question!]

就这些了,我希望您喜欢 C++ 的一点历史。

关于c++ - 为什么 STL 数值算法使用 'op' 而不是 'op=' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28862947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com