Sign-up....

怎样查找链表中 某对象的属性 符合特定的值?

[以下代码用于说明问题,可能有错误]

比如:

class Point {

public:

Point(_x, _y) : x(_x), y(_y) {}

int x, y;

};

void main() {

std::list<Point> PointList;

for (int i = 0; i < 30; i++)

Point p(i, i+100);

PointList.push_back(p);

}

//问题: 怎么样用泛型算法找出PointList x=22 的那个Point对象.

// 条件: 要求 1.只用标库的查找算法。

2.不可以自定义仿函式(函数对象)。

//伪代码如下:

Point p& = Find_In_PointList(PointList.begin(), PointList.end(), SearchFunctor(x == 22));

}

谢谢帮忙。

[455 byte] By [msdn] at [2007-8-14 12:57:48]
# 1 Re: 怎样查找链表中 某对象的属性 符合特定的值?

#include<list>

class Point {

public:

Point(int _x,int _y) : x(_x), y(_y) {}

int x, y;

};

void main() {

std::list<Point> PointList;

for (int i = 0; i < 30; i++)

{

Point p(i, i+100);

PointList.push_back(p);

}

}

xteaj at 2004-10-6 10:52:37 >
# 2 Re: 怎样查找链表中 某对象的属性 符合特定的值?

to xteaj(半桶水) 兄:

你好象没有回答我的问题啊 :)

bigtree21cn at 2004-10-6 10:55:15 >
# 3 Re: 怎样查找链表中 某对象的属性 符合特定的值?

它告诉你的是:你存在一个错误

错误的改成他给你了

UPCC at 2004-10-6 10:56:35 >
# 4 Re: 怎样查找链表中 某对象的属性 符合特定的值?

int x(std::list<Point> p)

{

return p.x;

}

Point *p = find(PointList.begin(),PointList.end(),22);

xteaj at 2004-10-6 11:04:19 >
# 5 Re: 怎样查找链表中 某对象的属性 符合特定的值?

int x(Point p)//对不起写错了

{

return p.x;

}

Point *p = find(PointList.begin(),PointList.end(),22);

xteaj at 2004-10-6 11:05:49 >
# 6 Re: 怎样查找链表中 某对象的属性 符合特定的值?

GX楼上

UPCC at 2004-10-6 11:24:57 >
# 7 Re: 怎样查找链表中 某对象的属性 符合特定的值?

三井的更符合要求。不过希望三井能改一下名字,我一看见日本名字就觉得恶心。

如果真是日本人,那...........

xteaj at 2004-10-6 11:30:55 >
# 8 Re: 怎样查找链表中 某对象的属性 符合特定的值?

其实我的意思是 怎样 不定义仿函式或重载操作符 怎样查找一个对象中的字断。

即类似下面的功能。

list<Point>::iterator result = find(PointList.begin(),PointList.end(),Point::x);

不知道有没有这个功能。 不知道结合 mem_ref_t 和 bind函数能不能实现这个功能。

虽然这不符合我的要求,不过还是谢谢大家关注。

结贴。

bigtree21cn at 2004-10-6 13:15:47 >

C/C++

All Classified