C++模板+迭代器(noob问题)

moo*_*rej 2 c++ templates iterator

我的免责声明是我在一周前开始自学C++课程,而我之前的编程经验是动态语言(Python,javascript).

我正在尝试使用通用函数迭代矢量的内容来打印出项目:

#include <iostream>
#include <algorithm>
#include <vector>

using std::vector;
using std::cout;

template <class T>
void p(T x){
    cout << x;
}

int main () {

    vector<int> myV;

    for(int i = 0; i < 10; i++){
        myV.push_back(i);
    }

    vector<int>::const_iterator iter = myV.begin();

    for_each(iter, myV.end(), p);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码无法编译.有人解释原因吗?

编辑:编译器错误:

error: no matching function for call to 'for_each(_gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<const int, _gnu_norm::vector<int, std::allocator<int> > >, __gnu_debug_def::vector<int, std::allocator<int> > >&, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int, __gnu_norm::vector<int, std::allocator<int> > >, __gnu_debug_def::vector<int, std::allocator<int> > >, <unknown type>)'

谢谢!

Mar*_*ork 13

尝试:

for_each(myV.begin(), myV.end(), p<int>);
Run Code Online (Sandbox Code Playgroud)

你的代码中有两个错误:

  • 迭代器不是同一类型
  • 函数指针实际上不是指针.
    • 通常模板化的函数可以从那里参数推导出来.但在这种情况下,您实际上并没有使用它,而是将它(或其地址)传递给函数(因此模板函数推导的常规规则不起作用).由于编译器无法推断出您需要使用哪个版本的函数'p',因此您必须明确.

还有一个很好的输出迭代器可以做到这一点:

std::copy(myV.begin(),myV.end(), std::ostream_iterator<int>(std::cout));
Run Code Online (Sandbox Code Playgroud)

另请注意,很少有编译器可以在函数指针调用中优化代码.
虽然如果它是一个仿函数对象,大多数都能够优化调用.因此,以下可能是函数指针的可行替代方案:

template<typename T>
struct P
{
    void operator()(T const& value) const
    {
        std::cout << value;
    }
};

....

for_each(myV.begin(), myV.end(), P<int>());
Run Code Online (Sandbox Code Playgroud)

另一个注意事项:
使用模板化方法/函数时,通常使用const引用而不是值.如果要复制的类型很昂贵,那么按值传递将生成一个可能不符合预期的复制结构.

  • 可以补充一点,p <int>表示一个特定的函数,编译器可以为其生成代码.只写p不会说T是哪个类型(int,float或rhubarb_pie?),而c ++类型系统并没有推断出那么多(松散配方,它往往是自上而下,而不是自下而上). (4认同)