问题描述
您好,
我遇到以下问题:我有以下课程:
class reachGraphState {
受保护:
/ * ......有些东西...... * /
公开:
reachGraphState(stateType,bool);
reachGraphState(stateType,bool,State *);
~addoGraphState();
州*州;
静态bool sortReachGraphState(const reachGraphState& left,const
reachGraphState& right){
left.state< right.state;
}
朋友布尔运算符==(const reachGraphState& s1,const
reachGraphState& s2);
朋友布尔运营商< (const reachGraphState& s1,const
reachGraphState& s2);
朋友ostream&运算符<< (std :: ostream& os,const
reachGraphState& s);
};
然后我有另一个班级,其中包含一组
reachGraphState-对象。所以我做了以下事情:
class stateList {
private:
/ * some stuff * /
public:
stateList();
~stateList();
set< reachGraphState * ,reachGraphState :: sortReachGraphState>
myStateSet;
};
但这不起作用。谁可以帮我这个事。如何使用某种排序功能声明
STL设置对象。
提前多多谢谢,
CQ 。
1.集合的排序函数必须接受集合的两个参数's
值类型,无论是价值还是参考价值。由于你的set的值类型是
指向reachGraphState的指针,你需要一个排序函数,其中有两个(引用
to)到reachGrapState的指针,但是你指定的函数需要
引用reachGraphState对象,而不是指向这些对象的指针。你好b / b
没告诉我们什么行不通,但我猜你从编译器得到了一些错误信息
。
2. sortReachGraphState函数看起来很可疑。你真的
想要通过其成员之一的内存地址对reachClassState的实例进行排序吗?
Heinz
集合的比较函数的参数应与
中的元素具有相同的类型集合。如果你有一组reachGraphState',你需要一个
函数来比较两个reachGraphState',如果你有一套
指向reachGraphState'的话你需要一个比较这样的功能的指南:
bool less(reachGraphState const * lhs,reachGraphState const * rhs)
{
返回reachGraphState :: sortReachGraphState(* lhs,* rhs);
}
std :: map< reachGraphState *,less> myMap;
HTH
Heinz
Hi there,
I am having the following problem: I have the following class:
class reachGraphState {
protected:
/* ... some stuff .... */
public:
reachGraphState(stateType, bool);
reachGraphState(stateType, bool, State *);
~reachGraphState();
State * state;
static bool sortReachGraphState(const reachGraphState& left, const
reachGraphState& right) {
left.state < right.state;
}
friend bool operator == (const reachGraphState& s1, const
reachGraphState& s2);
friend bool operator < (const reachGraphState& s1, const
reachGraphState& s2);
friend ostream& operator << (std::ostream& os, const
reachGraphState& s);
};
And then I have another class, which shall contain a set of those
reachGraphState-objects. So I did the following:
class stateList {
private:
/* some stuff */
public:
stateList();
~stateList();
set<reachGraphState *, reachGraphState::sortReachGraphState>
myStateSet;
};
But this does not work. Can anyone help me on this. How do I declare an
STL set over objects using a certain sorting function.
Thanks a lot in advance,
CQ.
1. The sorting function for a set must accept two arguments of the set''s
value type, either by value or by reference. Since your set''s value type is
pointer to reachGraphState, you need a sorting function with two (references
to) pointers to reachGrapState, but the function you specified expects
references to reachGraphState objects, not pointers to such objects. You
didn''t tell us what didn''t work, but I assume you got some error message
from your compiler.
2. The sortReachGraphState function looks quite suspicious. Do you really
want to sort instances of reachClassState by the memory address of one of
their member?
Heinz
The parameters of the compare function of a set should have the same type as
the elements in the set. If you have a set of reachGraphState''s, you need a
function that compares two reachGraphState''s, and if you have a set of
pointers to reachGraphState''s you need a function that compares such
pointers:
bool less(reachGraphState const* lhs, reachGraphState const* rhs)
{
return reachGraphState::sortReachGraphState(*lhs, *rhs);
}
std::map<reachGraphState*, less> myMap;
HTH
Heinz
这篇关于使用STL设置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!