gpt4 book ai didi

c++ - STL 标准::转换

转载 作者:搜寻专家 更新时间:2023-10-31 00:27:52 25 4
gpt4 key购买 nike

transform() 算法有两种形式,我喜欢第一种。

这是第二个模板规范:

template <class InputIterator1, class InputIterator2,
class OutputIterator, class BinaryOperation>
OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op);

我的书说,

In the second form, the transformation is applied using a binary operator function that receives the value of an element from the sequence to be transformed as in its first parameter and an element from the second sequence as its second parameter.

Q1。为什么第二个序列没有输入迭代器参数指示第二个序列结束?即为什么 transform() 中没有 InputIterator last2 参数?

问题 2。当第一个和第二个序列的长度不相等时会发生什么?

问题 3。 block 引用的文本表示二元运算符函数。那么这个运算符函数在这种情况下意味着什么?任何二元函数都无效吗?

最佳答案

q1. Why is there no input iterator parameter for the second sequence indicating the end of second sequence?

因为无论如何第二个序列必须至少和第一个序列一样长。当到达第一个序列的末尾时,算法知道它的工作已经完成。

q2. What happens when the length of the first and second sequence is unequal?

如果第二个序列比第一个短,你会得到未定义的行为。否则,如果第二个更长,则不会发生任何问题;其余元素将被忽略。

q3. The blockquoted text says binary operator function. So what does that operator function mean in this context?

它的第一个参数是第一个序列中的元素N,它的第二个参数是第二个序列中的元素N。它可以是任何可以这样称呼的东西,例如一个普通的函数,一个类似函数的对象,比如 std::function,一个 lambda,通过 std::bind 绑定(bind)的东西等等。唯一重要的是它可以用 () 语法调用。

我推荐一些关于“函数对象”和“仿函数”的互联网研究。

关于c++ - STL 标准::转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47366436/

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