使用STL设置对象 [英] using STL set with objects

查看:86
本文介绍了使用STL设置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我遇到以下问题:我有以下课程:


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 。

解决方案

" CQ" <苏******* @ gmx.net> schrieb im Newsbeitrag

新闻:11 ********************** @ o13g2000cwo.googlegr oups.com ...

你好,

我遇到以下问题:我有以下课程:

class reachGraphState {
/ * ...一些东西.... * /

状态*状态;

静态bool sortReachGraphState(const reachGraphState& left,const
reachGraphState& right){
left .state< right.state;
}
};

然后我有另一个类,它将包含一组
reachGraphState-对象。所以我做了以下工作:

class stateList {
private:
/ * some stuff * /

set< reachGraphState *,reachGraphState :: sortReachGraphState> ;
myStateSet;
};

但这不起作用。谁可以帮我这个事。如何使用某个排序函数声明对象的STL设置。




1.集合的排序函数必须接受集合的两个参数's
值类型,无论是价值还是参考价值。由于你的set的值类型是

指向reachGraphState的指针,你需要一个排序函数,其中有两个(引用

to)到reachGrapState的指针,但是你指定的函数需要

引用reachGraphState对象,而不是指向这些对象的指针。你好b / b
没告诉我们什么行不通,但我猜你从编译器得到了一些错误信息




2. sortReachGraphState函数看起来很可疑。你真的

想要通过其成员之一的内存地址对reachClassState的实例进行排序吗?


Heinz


我收到以下错误消息:


错误:模板参数列表中参数2的类型/值不匹配

表示`template< class _Key,class _Compare,class _Alloc> class std :: set''


是的,我想按成员州的记忆地址排序。


可以你给我一个例子,看看排序功能应该是什么样的。我需要通过哪些参数?

非常感谢,


CQ。


" CQ" <苏******* @ gmx.net> schrieb im Newsbeitrag

新闻:11 ********************** @ g43g2000cwa.googlegr oups.com ...

我收到以下错误消息:

错误:模板参数列表中参数2的类型/值不匹配
fortemplate< class _Key,class _Compare,class _Alloc> class std :: set''

是的,我想按成员州的记忆地址排序。

你能给我一个例子说明如何排序功能应该看起来像。我必须通过哪些参数?




集合的比较函数的参数应与

中的元素具有相同的类型集合。如果你有一组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.

解决方案

"CQ" <su*******@gmx.net> schrieb im Newsbeitrag
news:11**********************@o13g2000cwo.googlegr oups.com...

Hi there,

I am having the following problem: I have the following class:

class reachGraphState {
/* ... some stuff .... */

State * state;

static bool sortReachGraphState(const reachGraphState& left, const
reachGraphState& right) {
left.state < right.state;
}
};

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 */

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.



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


I get the following error message:

error: type/value mismatch at argument 2 in template parameter list
for `template<class _Key, class _Compare, class _Alloc> class std::set''

Yes, I want to sort by the memory address of the member "state".

Could you give me an example of how the sorting function should look
like. What parameters do I have to pass?
Thanks a lot,

CQ.


"CQ" <su*******@gmx.net> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...

I get the following error message:

error: type/value mismatch at argument 2 in template parameter list
for `template<class _Key, class _Compare, class _Alloc> class std::set''

Yes, I want to sort by the memory address of the member "state".

Could you give me an example of how the sorting function should look
like. What parameters do I have to pass?



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设置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆