怎样查找链表中 某对象的属性 符合特定的值?
[以下代码用于说明问题,可能有错误]
比如:
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 >

# 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 >

# 8 Re: 怎样查找链表中 某对象的属性 符合特定的值?
其实我的意思是 怎样 不定义仿函式或重载操作符 怎样查找一个对象中的字断。
即类似下面的功能。
list<Point>::iterator result = find(PointList.begin(),PointList.end(),Point::x);
不知道有没有这个功能。 不知道结合 mem_ref_t 和 bind函数能不能实现这个功能。
虽然这不符合我的要求,不过还是谢谢大家关注。
结贴。