#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//函数对象做参数 做返回值
class myprint04{
public:
myprint04() :count(0){}
void operator()(int v){
count++;
cout << v << " ";
}
int count;
};
void test04(){
vector<int> v;
for (int i = 0; i < 10;i++){
v.push_back(i);
}
myprint04 myp04;
myprint04 myp05 = for_each(v.begin(), v.end(), myp04); //函数对象做参数
cout << "count:" << myp04.count << endl; cout << endl;
cout << "count:" << myp05.count << endl; cout << endl;
}
int main(){
test04();
return 0;
}
0 1 2 3 4 5 6 7 8 9 count:0
count:10
我不理解的是myp04.count为什么是0.既然是0,我断点调试也发现对象成员
变量的值的确没变。可是不知道为什么,谁能说一下原理吗