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 这篇关于另一种地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 03:59