Initializing a map...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barcaroller

    Initializing a map...


    Is there a way in C++ to initialize an STL map in one statement (the way
    arrays can be initialized in C)?

    For example, instead of using:

    map<type1,type2 mymap;
    mymap[key1] = value1;
    mymap[key2] = value2;


    I would like to use something like:

    // wrong syntax!
    map<type1,type2 mymap = { (key1, value1), (key2, value2) };




  • Jeff Schwab

    #2
    Re: Initializing a map...

    barcaroller wrote:
    Is there a way in C++ to initialize an STL map in one statement (the way
    arrays can be initialized in C)?
    >
    For example, instead of using:
    >
    map<type1,type2 mymap;
    mymap[key1] = value1;
    mymap[key2] = value2;
    >
    >
    I would like to use something like:
    >
    // wrong syntax!
    map<type1,type2 mymap = { (key1, value1), (key2, value2) };
    There's no special syntax for maps. You do have a few options, though.
    One is to initialize an array with the nicer syntax, then initialize
    the map from the array.

    typedef std::map<type1, type2map_type;
    typedef map_type::value _type pair_type;

    template<typena me T, std::size_t z>
    std::size_t size(T const (&a)[z]) {
    return z;
    }

    int main() {
    pair_type initializers[] =
    { pair_type(key1, value1), pair_type(key2, value2) };
    map_type m(initializers, initializers + size(initialize rs));
    }

    Another option is to create the map within a function, then return it by
    value.

    map_type create_map() {
    map_type result;
    result.insert(p air_type(key1, value1));
    result.insert(p air_type(key2, value2));
    return result;
    }

    int main() {
    map_type map = create_map();
    }

    A third option is to let the map start out empty, then use a function to
    populate it.

    void populate(map_ty pe& m) {
    m.insert(pair_t ype(key1, value1));
    m.insert(pair_t ype(key2, value2));
    }

    int main() {
    map_type m;
    populate(m);
    }

    Comment

    • Jim Langston

      #3
      Re: Initializing a map... [OT] to sam

      Your messages always have the text as an attachment instead of in the body
      of the message. Just thought you might want to know.

      --
      Jim Langston
      tazmaster@rocke tmail.com
      "Sam" <sam@email-scan.comwrote in message
      news:cone.12035 53586.946080.99 50.500@commodor e.email-scan.com...


      Comment

      • James Kanze

        #4
        Re: Initializing a map... [OT] to sam

        On Feb 21, 4:22 am, "Alf P. Steinbach" <al...@start.no wrote:
        * Jim Langston:
        Your messages always have the text as an attachment instead
        of in the body of the message. Just thought you might want
        to know.
        Only for Outlook Express users... ;-)
        Or Google groups.
        It seems Outlook Express is the only commonly used newsreader
        that doesn't handle digitally signed messages correctly.
        For what definition of "correctly" ? According to the RFC,
        attachments are not allowed in news, so arguably, "correctly"
        would mean not displaying or propagating the message:-).
        (Realistically, of course: the first rule is always "be liberal
        in what you accept, and conservative in what you send". No good
        newsreader would ever append an attachment, but all good
        newsreaders would accept it. And allowing at least a digital
        signature does seem a reasonable extension to me.)

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Sam

          #5
          Re: Initializing a map...

          Jeff Schwab writes:
          >>
          >#include <iostream>
          >#include <ostream>
          >>
          >int main()
          >{
          > std::map<int, intm(map_initia lizer(3,4)(5,6) (7,8));
          ...
          > return 0;
          >}
          >
          That's a neat idea. It could probably be made a little more efficient
          by replacing the calls to map::operator[] with calls to map::find and
          operator[] takes only one argument. You can use a std::pair, but it'll make
          this even more ugly.



          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.7 (GNU/Linux)

          iD8DBQBHvWo7x9p 3GYHlUOIRAnzkAJ 9rd3VYOlnl6mk9f 6Nk2mVwbrt4/QCbBVn+
          wvGArnNFpaAb6SH W9LJnAJU=
          =2p1k
          -----END PGP SIGNATURE-----

          Comment

          • Jeff Schwab

            #6
            Re: Initializing a map...

            James Kanze wrote:
            On Feb 21, 1:22 am, Jeff Schwab <j...@schwabcen ter.comwrote:
            >barcaroller wrote:
            >>Is there a way in C++ to initialize an STL map in one
            >>statement (the way arrays can be initialized in C)?
            >
            >>For example, instead of using:
            >
            >> map<type1,type2 mymap;
            >> mymap[key1] = value1;
            >> mymap[key2] = value2;
            >
            >>I would like to use something like:
            >
            >> // wrong syntax!
            >> map<type1,type2 mymap = { (key1, value1), (key2, value2) };
            >
            >There's no special syntax for maps.
            >
            I think there will be in the next version of the standard. (I
            know that there was a proposal for extended initializers, but
            I'm not sure what the current status of the proposal was.)
            The proposal:


            I didn't know about that. Very neat. I'm not thrilled with g++ warning
            me about:

            std::tr1::array <int, 42= { 0 }; // g++ wants { { 0 } }

            It would bother me less if there were consistent syntax for primitive
            and UD types.

            >You do have a few options, though.
            >
            > One is to initialize an array with the nicer syntax, then initialize
            >the map from the array.
            >
            This is the only way to create a const map.
            That's a very good point, and is the reason my "not really
            initialization" solutions are inferior.
            > int main() {
            > pair_type initializers[] =
            > { pair_type(key1, value1), pair_type(key2, value2) };
            > map_type m(initializers, initializers + size(initialize rs));
            > }
            >
            I often find it worthwhile to define a special structure for
            this, something along the lines of:
            >
            typedef std::map< std::string, double Map ;
            struct MapInit
            {
            char const* key ;
            double value ;
            operator Map::value_type () const
            {
            return Map::value_type ( std::string( key ), value ) ;
            }
            } ;
            Is the key stored as a char const* so that construction of the
            initializers does not require any run-time overhead? Does MapInit count
            as a POD type, and is there benefit to using POD initializers?

            Comment

            Working...