另一种地图? [英] The other kind of map?

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

问题描述

可能有很多方法可以做到这一点,但我想知道有一个

C ++约定。我希望在键和值之间有一个固定的映射。

它与std :: map<>基本相同,除了我在说什么

about不会在miss上创建一个新元素。这是我用C和其他语言看到的传统

方法。

string

SectionTypes(Elf32_Word类型)

{

string sRet =" UNKNOWN";

switch(type){

case SHT_NULL:

sRet =" NULL";

break;

case SHT_PROGBITS:

sRet =" PROGBITS";

休息;

案例SHT_SYMTAB:

sRet =" SYMTAB";

休息;

case SHT_STRTAB:

sRet =" STRTAB";

break;

case SHT_RELA:

sRet =" ; RELA" ;;

休息;

案例SHT_HASH:

sRet =" HASH";

break;

案例SHT_DYNAMIC:

sRet =" DYNAMIC";

休息;

案例SHT_NOTE:

sRet =" NOTE";

break;

case SHT_NOBITS:

sRet =" NOBITS";

休息;

case SHT_REL:

sRet =" REL";

break;

case SHT_SHLIB:

sRet =" ; SHLIB" ;;

休息;

案例SHT_DYNSYM:

sRet =" DYNSYM";

break;

}


返回sRet;

}


如果我使用一个切换方法,我会从案例中返回而不是

从结尾掉下来。即使这对我来说似乎有点过于笨拙。 std :: map<>

适用于它的设计目的,但它不适用于具有

a默认值的固定设置没有关键可以匹配样品。


我知道这是微不足道的,但这是我发现自己烦恼的事情

over似乎有必要。每当我遇到这种情况时,我都会玩一个解决方案,但我从来没有找到过合适的。任何

建议一个漂亮,简洁,灵活的方式来设置这种

关联映射。


我可以使用一组std :: pair< key,value>,并提供一个比较器,但

对于这种情况似乎不必要地复杂化。使用std :: map<>

并且进行查找同样似乎过度。


-

如果我们的假设是关于任何东西,而不是关于某一个或多个特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell

There are probably a zillion ways to do this, but I''m wondering of there''s a
C++ convention. I want to have a fixed mapping between keys and values.
It''s basically the same thing as a std::map<>, except what I''m talking
about would not create a new element on a miss. This is a traditional
approach I''ve seen with C and other languages.
string
SectionTypes( Elf32_Word type )
{
string sRet = "UNKNOWN";
switch ( type ) {
case SHT_NULL :
sRet = "NULL";
break;
case SHT_PROGBITS :
sRet = "PROGBITS";
break;
case SHT_SYMTAB :
sRet = "SYMTAB";
break;
case SHT_STRTAB :
sRet = "STRTAB";
break;
case SHT_RELA :
sRet = "RELA";
break;
case SHT_HASH :
sRet = "HASH";
break;
case SHT_DYNAMIC :
sRet = "DYNAMIC";
break;
case SHT_NOTE :
sRet = "NOTE";
break;
case SHT_NOBITS :
sRet = "NOBITS";
break;
case SHT_REL :
sRet = "REL";
break;
case SHT_SHLIB :
sRet = "SHLIB";
break;
case SHT_DYNSYM :
sRet = "DYNSYM";
break;
}

return sRet;
}

If I were to use a switch method, I''d return from the case rather than
falling off the end. Even that seems a bit too clunky for me. std::map<>
is good for what it''s designed for, but it doesn''t work for fixed sets with
a default for cases where there''s no key to match the sample.

I know this is trivial, but it''s the kind of thing I find myself fretting
over more than seems necessary. Everytime I encounter the situation I toy
around with a solution, but I''ve never found the "right fit". Any
suggestions for a nice, concise, flexible way of setting up this kind of
associative mapping.

I could use a set of std::pair<key,value>, and provide a comparator, but
that seems unnecessarily complicated for the situation. Using a std::map<>
and doing a find likewise seems excessive.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell

推荐答案

* Steven T. Hatton:
* Steven T. Hatton:
std :: map<>
对它来说是好事设计用于,但它不适用于固定套件,默认情况下没有钥匙可以匹配样品。
std::map<>
is good for what it''s designed for, but it doesn''t work for fixed sets with
a default for cases where there''s no key to match the sample.




如何用自己的

访问器在你自己的小班中包装std :: map,你在哪里使用''find''?


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A :热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



How about wrapping a std::map in your own little class with your own
accessor, where you use ''find''?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alf P. Steinbach写道:
Alf P. Steinbach wrote:
* Steven T. Hatton:
* Steven T. Hatton:
std :: map<>
对于它的设计是好的,但它不适用于固定集
默认情况下没有它的情况匹配样本的关键。
std::map<>
is good for what it''s designed for, but it doesn''t work for fixed sets
with a default for cases where there''s no key to match the sample.



如何用自己的
访问器在你自己的小班中包装std :: map,你在哪里使用''find''?



How about wrapping a std::map in your own little class with your own
accessor, where you use ''find''?




我猜这个主题的变体是你在说什么。这是

中迄今为止最好的。


#ifndef FILEDUMPER_H

#define FILEDUMPER_H


#include< string>

#include< map>

#include< iostream>


类FileDumper {

public:


FileDumper(const std :: string& filename_);


~FileDumper();


std :: string SectionFlags(Elf32_Word flags);

std :: string SectionTypes(Elf32_Word类型) );

std :: string SegmentTypes(Elf32_Word类型);

// ...


private:

std :: string _filename;

std :: map< Elf32_Word,std :: string> _sectionTypes;

std :: map< Elf32_Word,std :: string> _segmentTypes;

};

#endif


/ *************** ********************************** /


FileDumper :: FileDumper(const std :: string& filename_)

:_filename(filename_){

_sectionTypes [SHT_NULL] =" NULL";

_sectionTypes [SHT_PROGBITS] =" PROGBITS";

_sectionTypes [SHT_SYMTAB] =" SYMTAB";

_sectionTypes [SHT_STRTAB] =" STRTAB";

_sectionTypes [SHT_RELA] =" RELA";

_sectionTypes [SHT_HASH] =" HASH";

_sectionTypes [SHT_DYNAMIC] =" DYNAMIC";

_sectionTypes [SHT_NOTE] =" NOTE";

_sectionTypes [SHT_NOBITS] =" NOBITS";

_sectionTypes [SHT_REL] =" REL" ;;

_sectionTypes [SHT_SHLIB] =" SHLIB";

_sectionTypes [SHT_DYNSYM] =" DYNSYM";


_segmentTypes [ PT_NULL] =NU LL" ;;

_segmentTypes [PT_LOAD] =" PT_LOAD";

_segmentTypes [PT_DYNAMIC] =" PT_DYNAMIC";

_segmentTypes [PT_INTERP ] =" PT_INTERP";

_segmentTypes [PT_NOTE] =" PT_NOTE";

_segmentTypes [PT_SHLIB] =" PT_SHLIB";

_segmentTypes [PT_PHDR] =" PT_PHDR";


}


FileDumper :: ~FileDumper(){}


std :: string FileDumper :: SectionFlags(Elf32_Word flags){

std :: string sRet ="" ;;

if(flags) &安培; SHF_WRITE){sRet + =" W" ;; }

if(flags& SHF_ALLOC){sRet + =" A" ;; }

if(flags& SHF_EXECINSTR){sRet + =" X" ;; }

返回sRet;

}


std :: string FileDumper :: SectionTypes(Elf32_Word类型){

return(_sectionTypes.end()!= _ sectionTypes.find(typ e))?_ sectionTypes [type]:

" UNKNOWN";

}


-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


Steven T. Hatton写道:
Steven T. Hatton wrote:
可能有很多方法可以做到这一点,但我想知道那里''' s
一个C ++约定。我希望在键和值之间有一个固定的映射。
它与std :: map<>基本相同,除了我正在说的
关于不会创建一个关于小姐的新元素。这是我用C和其他语言看到的传统方法。
字符串

[big switch snipped]
如果我使用切换方法,我会从案件中返回,而不是从结束时掉下来。即使这对我来说似乎有点过于笨拙。 std :: map<>
对于它的设计是好的,但它不适用于固定集
默认情况下没有匹配键的情况样本。

我知道这是微不足道的,但是我发现自己担心的事情似乎超出了必要的范围。每当我遇到这种情况时,我都会玩弄解决方案,但我从来没有找到过合适的。建议使用一种简洁,灵活的方法来设置这种关联映射。

我可以使用一组std :: pair< key,value> ,并提供一个比较器,但对于这种情况似乎不必要的复杂。使用
std :: map<>同样做一个发现似乎过分。
There are probably a zillion ways to do this, but I''m wondering of there''s
a
C++ convention. I want to have a fixed mapping between keys and values.
It''s basically the same thing as a std::map<>, except what I''m talking
about would not create a new element on a miss. This is a traditional
approach I''ve seen with C and other languages.
string
[big switch snipped]
If I were to use a switch method, I''d return from the case rather than
falling off the end. Even that seems a bit too clunky for me. std::map<>
is good for what it''s designed for, but it doesn''t work for fixed sets
with a default for cases where there''s no key to match the sample.

I know this is trivial, but it''s the kind of thing I find myself fretting
over more than seems necessary. Everytime I encounter the situation I toy
around with a solution, but I''ve never found the "right fit". Any
suggestions for a nice, concise, flexible way of setting up this kind of
associative mapping.

I could use a set of std::pair<key,value>, and provide a comparator, but
that seems unnecessarily complicated for the situation. Using a
std::map<> and doing a find likewise seems excessive.



怎么样:


#include< map>


模板< typename KeyType,

typename ValueType>

class dictionary {

public:


typedef KeyType key_type;

typedef ValueType value_type;


private:


typedef std :: map< key_type,value_type> map_type;


map_type数据;

value_type answer_default;


public:


字典(value_type default_value = value_type())

:data()

,answer_default(default_value)

{}


字典add_pair(key_type const& key,

value_type const& val)const {

字典结果(* this);

result.data [key] = val;

返回(结果);

}


value_type const& operator [](key_type const& key)const {

typename map_type :: const_iterator iter = data.find(key);

if(iter!= data.end ()){

return(iter-> second);

} else {

return(answer_default);

}

}


}; //字典<>

#include< iostream>

#include< string>


int main(void ){

字典< int,std :: string> dict =

字典< int,std :: string> ("!)

.add_pair(0," hello")

.add_pair(1,"")

.add_pair(2,world);


for(int i = 0; i< 5; ++ i){

std :: cout<< dict [i];

}

std :: cout<< ''\ n'';

}

最好


Kai-Uwe Bux


What about:

#include <map>

template < typename KeyType,
typename ValueType >
class dictionary {
public:

typedef KeyType key_type;
typedef ValueType value_type;

private:

typedef std::map< key_type, value_type > map_type;

map_type data;
value_type answer_default;

public:

dictionary ( value_type default_value = value_type() )
: data ()
, answer_default ( default_value )
{}

dictionary add_pair ( key_type const & key,
value_type const & val ) const {
dictionary result ( *this );
result.data[key] = val;
return( result );
}

value_type const & operator[] ( key_type const & key ) const {
typename map_type::const_iterator iter = data.find( key );
if ( iter != data.end() ) {
return( iter->second );
} else {
return( answer_default );
}
}

}; // dictionary<>
#include <iostream>
#include <string>

int main ( void ) {
dictionary< int, std::string > dict =
dictionary<int,std::string> ( "!" )
.add_pair( 0, "hello" )
.add_pair( 1, " " )
.add_pair( 2, "world" );

for ( int i = 0; i < 5; ++i ) {
std::cout << dict[i];
}
std::cout << ''\n'';
}
Best

Kai-Uwe Bux


这篇关于另一种地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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