やあみんな選挙には行ったかな。今回の参議院選挙も投票率は50%を切っていてつまり国民の半分が政治に参加してないという状態なんだね。今回は、ポインタ値を、std::map
- 指定したキーに対するエントリが見つからなかった場合は、例外を投げたいことがあったりなかったり。
- エントリが見つかった場合は、その値を、見つからなかった場合は、0を返す検索メソッド。
ソースは以下のようになった。
namespace pocMapUtil { // マップにキーで検索かけます。 // 見つからなければ例外を投げるかどうかを指定できます。 // 反復子が帰ります。 template < class Key, class Val > typename map< Key, Val >::const_iterator SearchIter( const map< Key, Val >& mp, const Key& key, bool throw_exception=!0 ) { map< Key, Val >::const_iterator it = mp.find( key ); if( it==mp.end() ){ if( throw_exception ){ string str("pocSearchMapIter:: no hit for the key " ); str += key; throw std::exception( str.c_str() ); } } return it; } // マップに対し、キーのエントリがあるかどうか調べ、 // 見つかればその second 値を、見つからなければ 0 を返します。 // (second値がポインタの時のみ想定してます。) template < class Key, class Val > Val Search( const map< Key, Val >& mp, const Key& key, bool throw_exception=!0 ) { map< Key, Val >::const_iterator it = SearchIter( mp, key, throw_exception ); return (it==mp.end() )? 0: it->second; } // マップに対し、キーのエントリがあるかどうか調べ、 // 見つかれば erase() します。 template < class Key, class Val > void Erase( map< Key, Val >& mp, const Key& key, bool throw_exception=!0 ) { map< Key, Val >::const_iterator it = SearchIter( mp, key, throw_exception ); if( it!=mp.end() ){ mp.erase( it ); } } };
そしてこれを使って以前紹介した、pocNameMappedFactoryを書き換えたのがこちらだ。
どうだろう。ありきたりのコード、数行でも毎回書くのはバカらしいからこれも一つの重要な抽象化だね。