有时,我需要一些functor-helper来操作列表.我尽量将范围保持在本地范围内.
#include <iostream> #include <algorithm> using namespace std; int main() { struct Square { int operator()(int x) { return x*x; } }; int a[5] = {0,1,2,3,4}; int b[5]; transform(a,a+5,b,Square()); for(int i=0; i<5; i++) cout<<a[i]<<" "<<b[i]<<endl; }
hello.cpp: In function ‘int main()’: hello.cpp:18:34: error: no matching function for call to ‘transform(int [5],int*,int [5],main()::Square)’
如果我将Square移出main(),那没关系.