c++ - 关于仿函数的问题

标签 c++

这个问题的正确答案是 D,但我感到困惑的是不知道类中的添加操作究竟是什么时候发生的?如何添加数字?

Consider the following partial C++ code:

#include <vector>
#include <iostream>
using namespace std;

template<class Iter, class Formatter>
void mystery(Iter front, Iter end, Formatter doSomething)
{
    while (front != end)
    {
        doSomething(*front, *end):
        front++; end--;
    }
}
template <class T> class dunno
{
public:
    void operator()(T & a, T & b)
    {
        a = a + b;
    }
}
int main()
{
    vector<int> v; ... // insert an odd number of elements into the vector
    vector<int>::iterator it1 - v.begin();
    vector<int>::iterator it2 - v.end();
    it2--;
    dunno<int> d;
    mystery<vector<int>::iterator, dunno<int> >(itl, it2, d);
    for (int i = 0; i < v.size(); i++) cout << v[i] << endl;
    return 0;
}

If you assume that all iterators are bidirectional and non-constant, which of the following statements is true?

(a) This code does not compile because of a type mismatch in the mystery parameter list.

(b) This code does not compile because of a syntax error in the template instantiation for mystery.

(c) If the vector consists of the integers 1, 2, 3, 4, 5, 6, 7 in that order, with the first item on the left, then the output is 1, 2, 3, 4, 5, 6, 7. (i.e. the code dods not change the vector.)

(d) If the vector consists of the integers 1, 2, 3, 4, 5, 6, 7 in that order, with the first item on the left, then the output is 8, 8, 8, 4, 5, 6, 7.

(e) None of these options describes the behavior of this code.

最佳答案

当我们调用 mystery() 时

   | 1 | 2 | 3 | 4 | 5 | 6 | 7 |

     ^                       ^
     |                       |
   front                    end


front   is from v.begin();
end     is from v.end() (note: this is one past the end)
        followed by a -- operator that moves it back one.

现在我们开始循环:

while(front != end)

所以这将在它们不相等时循环。并在循环内同时移动:

front++; end--;

所以每次迭代看起来像这样:

Iteration 1:
   | 1 | 2 | 3 | 4 | 5 | 6 | 7 |

     ^                       ^
     |                       |
   front                    end

Iteration 2:
   | 1 | 2 | 3 | 4 | 5 | 6 | 7 |

         ^               ^
         |               |
       front            end

Iteration 3:
   | 1 | 2 | 3 | 4 | 5 | 6 | 7 |

             ^       ^
             |       |
           front    end

The loop is not entered.
   | 1 | 2 | 3 | 4 | 5 | 6 | 7 |

                 ^
                 |
           front   end

所以每次通过循环我们也做:

doSomething(*front, *end):

现在 doSomething 是 Formatter 类型(模板参数)的对象,它解码为 dunno 类型,它具有方法 void operator()(T & a, T & b) 之后替换模板参数 T 我们得到 void operator()(int & a, int & b)

所以行:

doSomething(*front, *end):

是语法糖:

doSomething.operator()(*front, *end):

调用此运算符的效果是将取消引用的迭代器作为参数传递。这实际上等同于:

Iteration 1:
    v[0]    = v[0] + v[6]; // Note v[0] == 1 v[6] == 7 etc.
  //v[0]    =  1   +   7;

Iteration 2:
    v[1]    = v[1] + v[5];
  //v[1]    =  2   +   6

Iteration 3:
    v[2]    = v[2] + v[4];
  //v[2]    =  3   +   5

因此,如果您的起始数组是:

1 2 3 4 5 6 7

最终结果是:

8 8 8 4 5 6 7

关于c++ - 关于仿函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5546536/

相关文章:

c++ - 当对象超出范围时是否调用析构函数?

c++ - WTF WTF 在 WebKit 代码库中代表什么?

c++ - 在 OpenCV 上使用 GPU 时如何确定线程数?

c++ - 如何避免来自 C 中的#define 的名称冲突? (或 C++)

c++ - strlen() 不工作

c++ - MSVC SAL 与 C++2a 契约

c++ - 发布 Qt C++ GUI 程序的最简单方法?

c++ - 有没有等同于 void* 的固定大小?

c++ - Xcode 4 菜单 "Build"和 "Clean"选项已禁用

c++ - 是否有用于 double 平方根倒数的快速 C 或 C++ 标准库函数?