********还是泛型算法问题**********

希望之晨 2006-06-10 11:19:43
class Condition
{
public:
string szField;
string szOper;
string szValue;
int iCount;

};

list<Condition> listCon;

我想把listCon 中符合条件(Condition 结构中 如szField = id, szValue >"100"等 .条件判断可能比较复杂)的iCount 值+1 不遍历list 用泛型算法能搞定么? 
曾经我定义过类函数(Fun),然后用find_if(listCon.begin(),listCon.end(),Fun(szPar1,szPar2)).但是只能返回第一个符合条件的iterator ,但我目的是要把所有符合条件的都(iCount+1),大家有什么办法么?

...全文
243 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bjzhaoxiao 2006-06-13
  • 打赏
  • 举报
回复
Before.....
cndt.nValue = 0cndt.iCount = 0
cndt.nValue = 1cndt.iCount = 0
cndt.nValue = 2cndt.iCount = 0
cndt.nValue = 3cndt.iCount = 0
cndt.nValue = 4cndt.iCount = 0
cndt.nValue = 5cndt.iCount = 0
cndt.nValue = 6cndt.iCount = 0
cndt.nValue = 7cndt.iCount = 0
cndt.nValue = 8cndt.iCount = 0
cndt.nValue = 9cndt.iCount = 0
After.....
cndt.nValue = 0cndt.iCount = 0
cndt.nValue = 1cndt.iCount = 0
cndt.nValue = 2cndt.iCount = 0
cndt.nValue = 3cndt.iCount = 0
cndt.nValue = 4cndt.iCount = 0
cndt.nValue = 5cndt.iCount = 0
cndt.nValue = 16cndt.iCount = 0
cndt.nValue = 17cndt.iCount = 0
cndt.nValue = 18cndt.iCount = 0
cndt.nValue = 19cndt.iCount = 0
Press any key to continue
bjzhaoxiao 2006-06-13
  • 打赏
  • 举报
回复
传引用是可以的。
bjzhaoxiao 2006-06-13
  • 打赏
  • 举报
回复
#include <list>
#include <string>
#include <functional>
#include <algorithm>

#include <iostream>

using namespace std;

class Condition
{
public:
//string szField;
//string szOper;
//string szValue;
int nValue;
int iCount;

};

list<Condition> listCon;

void PRINT(class Condition cndt)
{
cout<<"cndt.nValue = "<<cndt.nValue
<<"cndt.iCount = "<<cndt.iCount<<endl;

return;
}

// Attention:
// Should use ref. param.
void Fun(class Condition& cndt)
{
if( cndt.nValue > 5)
cndt.nValue += 10;

return;
}

void main()
{
Condition cndt;
for(int i = 0; i< 10; i++)
{
cndt.nValue = i;
cndt.iCount = 0;
listCon.push_back(cndt);
}
cout<<"Before....."<<endl;
for_each(listCon.begin(),listCon.end(),PRINT);

for_each(listCon.begin(),listCon.end(),Fun);

cout<<"After....."<<endl;
for_each(listCon.begin(),listCon.end(),PRINT);
return;
}
希望之晨 2006-06-11
  • 打赏
  • 举报
回复
to howyougen:
不直接遍历,要不然代码量太大,且层次不分明.
pottichu 2006-06-10
  • 打赏
  • 举报
回复
for_each
cunsh 2006-06-10
  • 打赏
  • 举报
回复

for_each()可以.
犀利鱼鱼 2006-06-10
  • 打赏
  • 举报
回复
struct functor
{
void operator() (Condition& condition)
{
if (......)
++condition.iCount;
}
};

for_each(listCon.begin(), listCon.end(), functor());

这样是不是可以?
xlsue 2006-06-10
  • 打赏
  • 举报
回复
飘过~
howyougen 2006-06-10
  • 打赏
  • 举报
回复
不遍历list 用泛型算法搞定???

对于list,使用泛型算法肯定是遍历list的
cnhgj 2006-06-10
  • 打赏
  • 举报
回复
传引用就可以..

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧