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)
你的代码中有两个错误:
还有一个很好的输出迭代器可以做到这一点:
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引用而不是值.如果要复制的类型很昂贵,那么按值传递将生成一个可能不符合预期的复制结构.
| 归档时间: |
|
| 查看次数: |
1321 次 |
| 最近记录: |