std :: map替换 [英] std::map replacement

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

问题描述

如果你喜欢std :: map的功能,但发现你

不能相信你的实现来处理非标准数据

结构,你会怎么做?编码包装?我已经制作了以下




#include< map>


template< class keytype,class recordtype>

class pseudomap

{

private:

map< keytype,unsigned int> maptable;

CustomList< recordtype> recordlist; //自定义数组类

//方法包括

//下面使用的那些


public:

void clear(){maptable.clear(); recordlist.Clear();}


recordtype& operator [](const keytype& key)

{

if(maptable.count(key)){

return(* recordlist [maptable] [key]]);

}

recordlist.Add(new recordtype());

maptable [key] = recordlist.Count- 1;

返回(* recordlist [maptable [key]]);

}

};


我觉得有很多批评代码

以上(虽然看起来工作得很好) - 所以我在听:)

毋庸置疑,并非所有的地图功能都包含在内 - 但它足以满足我目前的需求......


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

If you liked the functionality of std::map, but found that you
couldn''t trust your implementation to handle nonstandard data
structures, how would you code a wrapper? I''ve produced the
following:

#include <map>

template < class keytype, class recordtype >
class pseudomap
{
private:
map< keytype, unsigned int > maptable;
CustomList<recordtype> recordlist; // custom array-like class
// with methods including
// those used below

public:
void clear() {maptable.clear();recordlist.Clear();}

recordtype& operator[] ( const keytype& key )
{
if( maptable.count(key) ) {
return( *recordlist[maptable[key]] );
}
recordlist.Add( new recordtype() );
maptable[key]=recordlist.Count-1;
return( *recordlist[maptable[key]] );
}
};

I get the feeling that there is a lot to criticize about the code
above (although it seems to work just fine) - so I''m listening :)
Needless to say not all map functionality is included - but it''s good
enough for my current needs...

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写在

新闻:c7 ********** @ chessie.cirr.com:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c7**********@chessie.cirr.com:
如果你喜欢std的功能: :map,但发现你不相信你的实现来处理非标准的数据结构,你会如何编写一个包装器?我已经制作了
以下内容:


您称之为非标准数据结构?

#include< map>

模板< class keytype,class recordtype>
class pseudomap
{
private:
map< keytype,unsigned int> maptable;
CustomList< recordtype> recordlist; //自定义数组类
//方法包括
//下面使用的那些

公共:
void clear(){maptable.clear(); recordlist.Clear();}

recordtype& operator [](const keytype& key)
{
if(maptable.count(key)){
return(* recordlist [maptable [key]]);
}
recordlist.Add(new recordtype());
maptable [key] = recordlist.Count-1;
return(* recordlist [maptable [key]]);
}
};

我觉得有很多批评上面的代码(虽然看起来工作得很好) - 所以我在听:)
毋庸置疑,并非所有地图功能都包含在内 - 但它足以满足我目前的需求...
If you liked the functionality of std::map, but found that you
couldn''t trust your implementation to handle nonstandard data
structures, how would you code a wrapper? I''ve produced the
following:
What do you call "nonstandard data structures"?
#include <map>

template < class keytype, class recordtype >
class pseudomap
{
private:
map< keytype, unsigned int > maptable;
CustomList<recordtype> recordlist; // custom array-like class
// with methods including
// those used below

public:
void clear() {maptable.clear();recordlist.Clear();}

recordtype& operator[] ( const keytype& key )
{
if( maptable.count(key) ) {
return( *recordlist[maptable[key]] );
}
recordlist.Add( new recordtype() );
maptable[key]=recordlist.Count-1;
return( *recordlist[maptable[key]] );
}
};

I get the feeling that there is a lot to criticize about the code
above (although it seems to work just fine) - so I''m listening :)
Needless to say not all map functionality is included - but it''s good
enough for my current needs...




怎么能我们告诉?你没有说明你究竟想要什么来实现这个目标!



How can we tell? You haven''t stated what exactly you''re trying to
accomplish!


Christopher Benson-Manica< at ***@nospam.cyberspace.org>在消息新闻中写道:< c7 ********** @ chessie.cirr.com> ...
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<c7**********@chessie.cirr.com>...
如果你喜欢std :: map的功能,但发现你不能相信你的实现来处理非标准的数据结构,你会如何编写包装器?
If you liked the functionality of std::map, but found that you
couldn''t trust your implementation to handle nonstandard data
structures, how would you code a wrapper?




通过包装非标准数据或实施适当的

std :: less< non_standard_data> ;.


问候,

Michiel Salters



By wrapping the non-standard data or implementing a proper
std::less<non_standard_data>.

Regards,
Michiel Salters


Michiel Salters< Mi ************* @ logicacmg.com>这样说:
Michiel Salters <Mi*************@logicacmg.com> spoke thus:
通过包装非标准数据或实现正确的std :: less< non_standard_data>。
By wrapping the non-standard data or implementing a proper
std::less<non_standard_data>.




嗯,关键是unsigned int(用户定义的数据是

值),所以我不认为比较函数是问题。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Well, the key was an unsigned int (the user-defined data was the
value), so I don''t think the comparison function was the problem.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于std :: map替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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