map vs. set(stl) [英] map vs. set (stl)

查看:55
本文介绍了map vs. set(stl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有类似的东西,其中''name''是POD类型,而

''value''是一个类。

std :: map<名称,价值>


然后我意识到''name''实际上应该是''value''类的成员之一

,所以我有一个冗余。然后我使用std :: set<切换并启动

值>。为了使''价值''适合这个目的,我

使它看起来像这样...


类价值{

const int name;

bool operator<(const value& rhs)const

{return name< rhs.name; }

void operator =(const value& rhs);

...

};


现在满足了一套的要求,并且它有效。太棒了。

但我觉得我真的偏离了

套装的想法。例如,我的值的关键部分是不变的,但整个

值不是。


我真的应该使用这样的集合吗? br />

Let''s say I have something like this, where ''name'' is a POD type, and
''value'' is a class.

std::map< name, value >

But then I realize that ''name'' should actually be one of the members
of ''value'' class, so I have a redundancy. I then switch and start
using std::set< value >. To make ''value'' suitable for this purpose, I
make it look like this...

class value {
const int name;
bool operator<( const value& rhs ) const
{ return name < rhs.name; }
void operator=( const value& rhs );
...
};

This now satisfies the requirements of a set, and it works. Great.
But I feel as though I have really strayed far from the idea of a
set. For example, the key part of my value is constant, but the whole
value is not.

Should I really be using a set like this?

推荐答案

Qwavel写道:
Qwavel wrote:

让我们说我有像这样的东西,''name''是POD类型,

''value''是一个类。


std :: map<名称,价值>


然后我意识到''name''实际上应该是''value''类的成员之一

,所以我有一个冗余。然后我使用std :: set<切换并启动

值>。为了使''价值''适合这个目的,我

使它看起来像这样...


类价值{

const int name;

bool operator<(const value& rhs)const

{return name< rhs.name; }

void operator =(const value& rhs);

...

};


现在满足了一套的要求,并且它有效。太棒了。

但我觉得我真的偏离了

套装的想法。例如,我的值的关键部分是不变的,但整个

值不是。


我真的应该使用这样的集合吗? br />
Let''s say I have something like this, where ''name'' is a POD type, and
''value'' is a class.

std::map< name, value >

But then I realize that ''name'' should actually be one of the members
of ''value'' class, so I have a redundancy. I then switch and start
using std::set< value >. To make ''value'' suitable for this purpose, I
make it look like this...

class value {
const int name;
bool operator<( const value& rhs ) const
{ return name < rhs.name; }
void operator=( const value& rhs );
...
};

This now satisfies the requirements of a set, and it works. Great.
But I feel as though I have really strayed far from the idea of a
set. For example, the key part of my value is constant, but the whole
value is not.

Should I really be using a set like this?



听起来你只需要一个矢量......


F

Sounds like you just need a vector...

F


Qwavel写道:
Qwavel wrote:

假设我有这样的东西,其中''name''是POD类型,并且

''value''是一个类。


std :: map<名称,价值>


然后我意识到''name''实际上应该是''value''类的成员之一

,所以我有一个冗余。然后我使用std :: set<切换并启动

值>。为了使''价值''适合这个目的,我

使它看起来像这样...


类价值{

const int name;

bool operator<(const value& rhs)const

{return name< rhs.name; }

void operator =(const value& rhs);

...

};


现在满足了一套的要求,并且它有效。太棒了。

但我觉得我真的偏离了

套装的想法。例如,我的值的关键部分是不变的,但整个

值不是。


我真的应该使用这样的集合吗? br />
Let''s say I have something like this, where ''name'' is a POD type, and
''value'' is a class.

std::map< name, value >

But then I realize that ''name'' should actually be one of the members
of ''value'' class, so I have a redundancy. I then switch and start
using std::set< value >. To make ''value'' suitable for this purpose, I
make it look like this...

class value {
const int name;
bool operator<( const value& rhs ) const
{ return name < rhs.name; }
void operator=( const value& rhs );
...
};

This now satisfies the requirements of a set, and it works. Great.
But I feel as though I have really strayed far from the idea of a
set. For example, the key part of my value is constant, but the whole
value is not.

Should I really be using a set like this?



我认为它总是值得精炼你的设计,所以它很自然,所以

感觉是对的。你似乎只是在集合中的角色来考虑价值等级。尝试独立思考价值类

而不是如何在地图或集合中使用它。


你可能会发现一个自定义的比较器设置是要走的路。


john

I think it''s always worth refining your design so that it''s natural, so
it feels right. You seem to be thinking about the value class solely in
terms of it''s role within the set. Try thinking about the value class
independently instead of how you might be using it in a map or set.

You might find that a custom comparator for a set is the way to go.

john


2007年5月23日星期三12:41:18 -0700,Qwavel写道:
On Wed, 23 May 2007 12:41:18 -0700, Qwavel wrote:

让我们说我有这样的东西,其中''name''是POD类型,并且

' 'value''是一个类。


std :: map<名称,价值>


然后我意识到''name''实际上应该是

''value''类的成员之一,所以我有一个冗余。然后我切换并开始使用

std :: set<值>。为了使''价值''适合这个目的,我做到了

看起来像这样......


类价值{

const int name;

bool operator<(const value& rhs)const

{return name< rhs.name; }

void operator =(const value& rhs);

...

};


现在满足了一套的要求,并且它有效。大。但是

我觉得好像我真的偏离了一套的想法。对于

示例,我的值的关键部分是常量,但整个值是

不是。


我应该真的使用这样的一套?
Let''s say I have something like this, where ''name'' is a POD type, and
''value'' is a class.

std::map< name, value >

But then I realize that ''name'' should actually be one of the members of
''value'' class, so I have a redundancy. I then switch and start using
std::set< value >. To make ''value'' suitable for this purpose, I make it
look like this...

class value {
const int name;
bool operator<( const value& rhs ) const
{ return name < rhs.name; }
void operator=( const value& rhs );
...
};

This now satisfies the requirements of a set, and it works. Great. But
I feel as though I have really strayed far from the idea of a set. For
example, the key part of my value is constant, but the whole value is
not.

Should I really be using a set like this?



你可能遇到的问题是你不能(没有强制转换)

通过set迭代器修改集合中的对象。一个set迭代器是

基本上总是一个const迭代器来防止破坏

集的排序。


-

Markus Schoder

The problem you might be facing is that you cannot (without casting)
modify the objects in the set through a set iterator. A set iterator is
basically always a const iterator to prevent breaking the ordering of the
set.

--
Markus Schoder


这篇关于map vs. set(stl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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