C++ container classes that don't stink?

Dennis devel at muhlesteins.com
Mon Aug 15 16:10:05 MDT 2005


Dave Smith wrote:

>Does anyone know of a good C++ replacement fot the STL container classes?
>I am specifically tired of dealing with std::map because of the awful
>looking code it requires. This is some seriously ugly stuff:
>
><painful-code>
>map<Class1,map<Class2,Class3> >::iterator iter = myMap.find( someInstance );
>if( iter != myMap.end() ) {
>   map<Class2,Class3>::iterator subIter =
>         (*iter).second.find( someOtherInstance );
>   if( subIter != (*iter).second.end() ) {
>      ...
>   }
>}
></painful-code>
>
>  
>
Most people using STL in a major way, also use typedefs so that the code
isn't so ugly.
typedef map<Class1,map<Class2,Class3>> myMap;
typedef myMap::iterator mapItr;

If you do that, you can swap out the container relatively easily too.

-Dennis

><mild-rant>
>Why didn't the STL creators think to include an exists() method or a []
>operator that didn't actually create an entry on use?! Sometimes even Java
>looks good compared to the STL.
></mild-rant>
>
>I'm trying to create some hierarchical structures that look more or less
>like this in C++:
>
>Class1
>  |
>  +---> Class2
>  |       |
>  |       +---> Class3
>  |
>  +---> Class2
>  |       |
>  |       +---> Class3
>  + ...
>
>If I were using some scripting language like Perl or PHP, this would be a
>no-brainer, but since it's C++, I'm stuck with a brainer. Can anyone help
>bail me out of this syntactical misery? Please no language flames, and
>please only C++ answers.
>  
>
I think the typedef answer gets you a ways down the road :D

>Thanks in advance!
>
>--Dave
>.-----------------------------------.
>| This has been a P.L.U.G. mailing. |
>|      Don't Fear the Penguin.      |
>|  IRC: #utah at irc.freenode.net   |
>`-----------------------------------'
>  
>




More information about the PLUG mailing list