C++ Extensions (cppx) Documentation Internal Version
Main | Namespaces | Classes | NamespaceMembers | ClassMembers | Files

stdxContainerInit.h Classes and Functions

Includes facilities for making initializing containers easier. The first overloads the += and , operators to enable the following syntax:

 std::vector<int> vec;
 vec += 1, 2, 3, 4, 5;

This works for any container, not just vectors, for which a push_back() method is defined. It will also work for an arbitrary number of elements.

The other technique is to use mk functions. These functions are supplied for one to twenty-five elements and enable the following syntax. Currently mkVec and mkLst functions are supplied. We could use a templated mk function so that this would work for any container, but this would probably make the syntax too verbose to be useful.

 std::list<int> lst = mkLst( 1, 2, 3, 4, 5 );

We use the preprocessor to generate these functions. Also note that if the literals are not exactly the right type then you might need to explicitly specify the type. For example:

 std::list<std::string> lst = mkLst<std::string>( "A", "B", "C" );

You can combine these two techniques to easily initialize two-dimensional vectors.

  vector< vector<int> > data;
  data += mkVec( 0, 1, 2, 3, 4, 5, 6 ),
          mkVec( 4, 5, 8, 3, 1, 9, 6 ),
          mkVec( 2, 3, 4, 3, 0, 9, 2 );

Compounds

Generated on Mon Aug 15 21:43:09 2005 by Doxygen 1.2.13-20020210 written by Dimitri van Heesch © 1997-2002