BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #85954同步于 2015/3/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[问题]set中保存自建类型的指针怎么实现不了find()?

liufangyu247
2015/3/15镜像同步12 回复
可以进行插入,我用迭代器看实现了我想要的排序,但是在查找的时候编译出问题,现实 不能将 const compare 转化为 compare &,请问何解? 程序类似这样 struct point { double priority; int UUID; point(int _UUID,double priority):UUID(_UUID),priority(_priority) {} }; struct compare { bool operator()(point * p1,point *p2) { if(p1->priority<p2->priority) return true; if(p1->priority==p2->priority) { if(p1->UUID<p2->UUID) return true; } return false; } }; void main() { set<point*,compare> Set; point * p,*p1; for(int i==0;i<10;++i) { p=new point(i,(rand()%10)/10.0 ); Set.insert(p); if(i==5) p1=p; } set<point*,compare>::iterator iter=Set.begin(); //排序没有问题 while(iter!=Set.end()) { cout<<(*iter)->priority<<" "<<(*iter)->UUID<<endl; ++iter; } //查找编译出错 Set.find(p1); } 求解啊,现在能想到的就是用map<point,point*>,但这样空间太大啊。
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
inaadversity机器人#1 · 2015/3/15
没有错啊 把出错信息贴出来看看
gaoweiwei机器人#2 · 2015/3/15
看了一眼,貌似分号中英文混搭,不知道是lz代码写错了还是发帖的时候弄错了
liufangyu247机器人#3 · 2015/3/15
【 在 gaoweiwei 的大作中提到: 】 : 看了一眼,貌似分号中英文混搭,不知道是lz代码写错了还是发帖的时候弄错了 手机码的。
liufangyu247机器人#4 · 2015/3/15
【 在 inaadversity 的大作中提到: 】 : 没有错啊 把出错信息贴出来看看 没有错吗?我这个程序我在vc6上会编出错,没有find()那句可以,有了就会出错,我去实验室再上图。
nuanyangyang机器人#5 · 2015/3/15
楼主试试C++11的lambda表达式? #include <iostream> #include <algorithm> #include <cstdlib> using namespace std; int ar[] = {-9,-7,-5,-3,-1,0,2,4,6,8}; int main() { sort(ar, ar+10, [](int a, int b){return abs(a)<abs(b);}); for(auto i : ar) { cout<<i<<endl; } return 0; }
liufangyu247机器人#6 · 2015/3/15
【 在 nuanyangyang 的大作中提到: 】 : 楼主试试C++11的lambda表达式? : [code=cpp] : #include <iostream> : ................... 只能用vc6……
nuanyangyang机器人#7 · 2015/3/15
【 在 liufangyu247 的大作中提到: 】 : 只能用vc6…… 为什么?
liufangyu247机器人#8 · 2015/3/15
【 在 inaadversity 的大作中提到: 】 : 没有错啊 把出错信息贴出来看看 #include<iostream> #include<set> using namespace std; struct CTracePoint { CTracePoint(int _UID,double _priority=0.0): UID(_UID),priority(_priority) { } int UID; double x; double y; double priority; }; ostream & operator<<(ostream &os,CTracePoint *p) { os <<" "<<"priority:"<<p->priority <<" "<<"UID:"<<p->UID <<" "<<"ADDRESS:"<<(void *)p // <<" "<<"x:"<<p->x // <<" "<<"y:"<<p->y ; return os; } struct compare { bool operator()(CTracePoint * val1,CTracePoint *val2) { if (val1->priority<val2->priority) { return true; } if (val1->priority==val2->priority) { if (val1->UID<val2->UID) { return true; } } return false; } }; //主程序这样,输出没问题 void main(int argc,char *argv[]) { set<CTracePoint *,compare> Set; static int UID=0; srand(NULL); CTracePoint *search; for (int i=0;i<20;++i) { CTracePoint *p=new CTracePoint(UID++,(rand()%10)/10.0); Set.insert(p); if(i==0) search=p; } set<CTracePoint *,compare>::iterator iter=Set.begin(); while (iter!=Set.end()) { cout<<*iter++<<endl; } //加这段显示错误: Set.find(search); /* d:\program files\microsoft visual studio\vc98\include\xtree(514) : error C2662: '()' : cannot convert 'this' pointer from 'const struct compare' to 'struct compare &' Conversion loses qualifiers d:\program files\microsoft visual studio\vc98\include\xtree(511) : while compiling class-template member function 'struct std::_Tree<struct CTracePoint *,struct CTracePoint *,struct std::set<struct CTracePoint *,struct compare,class std::allocator<struct CTracePoint *> >::_Kfn,struct compare,class std::allocator<struct CTracePoint *> >::_Node *__thiscall std::_Tree<struct CTracePoint *,struct CTracePoint *,struct std::set<struct CTracePoint *,struct compare,class std::allocator<struct CTracePoint *> >::_Kfn,struct compare,class std::allocator<struct CTracePoint *> >::_Lbound(struct CTracePoint *const & ) const' d:\program files\microsoft visual studio\vc98\include\xtree(514) : error C2064: term does not evaluate to a function d:\program files\microsoft visual studio\vc98\include\xtree(511) : while compiling class-template member function 'struct std::_Tree<struct CTracePoint *,struct CTracePoint *,struct std::set<struct CTracePoint *,struct compare,class std::allocator<struct CTracePoint *> >::_Kfn,struct compare,class std::allocator<struct CTracePoint *> >::_Node *__thiscall std::_Tree<struct CTracePoint *,struct CTracePoint *,struct std::set<struct CTracePoint *,struct compare,class std::allocator<struct CTracePoint *> >::_Kfn,struct compare,class std::allocator<struct CTracePoint *> >::_Lbound(struct CTracePoint *const & ) const' */ }
liufangyu247机器人#9 · 2015/3/15
我测试了下,比如: void main(int argc,char *argv[]) { set<CTracePoint *,compare> Set; static int UID=0; srand(NULL); CTracePoint *search; for (int i=0;i<20;++i) { CTracePoint *p=new CTracePoint(UID++,(rand()%10)/10.0); Set.insert(p); if(i==5) search=p; } cout<<"size:"<<Set.size()<<endl; Set.insert(new CTracePoint(search->UID,search->priority)); cout<<"size:"<<Set.size()<<endl; set<CTracePoint *,compare>::iterator iter=Set.begin(); while (iter!=Set.end()) { cout<<*iter++<<endl; } } 测试显示size没有变化,说明是可以检测相同的值的,虽然指针不同(但是实际中实现的时候可以保证UID的唯一性),那我就想问为什么find() count()都不可以实现呢?