I use an initializer class:
template<class K, class V>
class MapInitializer
{
std::map<K,V> m;
public:
operator std::map<K,V>() const
{
return m;
}
MapInitializer& Add( const K& k, const V& v )
{
m[ k ] = v;
return *this;
}
};
Then to put it to use:
class C
{
const std::map<int,const char*> m_map;
public:
C() : m_map( MapInitializer<int,const char*>()
.Add( 1, "Hi there" )
.Add( 2, "In the middle" )
.Add( 9, "Goodbye" ) )
{}
};
This is free (in the sense that you aren't building a map, copying it, and throwing the first away) because of C++'s return value optimization. The same thing can be done for initializing a vector or other standard containers.
Hope that helps!