- 论坛徽章:
- 0
|
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
struct Sub : public unary_function<int,void>
{
void operator() (int & ri)
{
ri -= 5;
}
};
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
copy(a,a+10,ostream_iterator<int>(cout," " );
cout << endl;
for_each(a,a+10,Sub());
copy(a,a+10,ostream_iterator<int>(cout," " );
cout << endl;
return 0
}
以上代码用g++编译通过,豪无问题
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <set>
using namespace std;
struct check_unary: public unary_function<int,void>{
void operator()(int& i){
i -= 5;
}
};
int main(){
set<int> c;
for(int i =0; i < 10; ++i)
c.insert(i);
for(set<int>::const_iterator pos = c.begin(); pos != c.end(); ++pos)
cout<< *pos<<' ';
cout<<endl;
for_each(c.begin(), c.end(), check_unary());
for(set<int>::const_iterator pos = c.begin(); pos != c.end(); ++pos)
cout<< *pos<<' ';
cout<<endl;
return 0;
}
但我将数组换成容器后就编译通不过了
报错为:
myfuc_practice.cpp:41: instantiated from here
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:159:
error: no match for call to ‘(check_unary) (const int& ’
myfuc_practice.cpp:27: note: candidates are: void check_unary: perator()(int&
才学stl的新手
求解!!!!!!
谢谢!!!!! |
|