您的位置:首页 > 其它

stl for_each某种用法

2011-05-17 12:30 375 查看
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

struct MyClass
{
int id;
bool flag;
MyClass(int iid):id(iid),flag(true){}
void operator()(MyClass& c){ if(c.id==this->id){c.flag=false;} }
};

int main(int, char *[])
{
vector<MyClass> tVector;
for(int i=0;i<10;i++){tVector.push_back(MyClass(i));}

for_each(tVector.begin(),tVector.end(),MyClass(2));

for (int i=0;i<tVector.size();i++){ cout << tVector[i].flag << endl;}
return 0;
};


这么写会遭人讨厌,但是种不错的思路,吼吼~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: