std::map initializer list syntax ?

This is a discussion on std::map initializer list syntax ? within the C++ Programming forums, part of the General Programming Boards category; C++11 supports the initialization of most containers by the simple syntax as follows: Code: std::set<int> = {3,4,6,34,2,6,78,8,5,0}; std::list<int> = {7,3,5,6,3,4,6,8}; ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,502

    std::map initializer list syntax ?

    C++11 supports the initialization of most containers by the simple syntax as follows:
    Code:
    std::set<int> = {3,4,6,34,2,6,78,8,5,0};
    std::list<int> = {7,3,5,6,3,4,6,8};
    Is there something equivalent to this for std::map `s which can be put outside all function code as the definition of the map ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, map also has a constructor that takes std::initializer_list<value_type>:

    Code:
    #include <map>
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::map<int, std::string> m{{1, "Hello"}, {2, "world"}, {4, "!!!"}};
        for (std::map<int, std::string>::const_iterator it = m.begin(); it != m.end(); ++it) {
            std::cout << it->first << ' ' << it->second << '\n';
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of initializer list - is it defined?
    By Elysia in forum C++ Programming
    Replies: 14
    Last Post: 03-26-2010, 03:10 PM
  2. 3D array initializer list
    By kenryuakuma in forum C# Programming
    Replies: 5
    Last Post: 11-15-2009, 01:40 AM
  3. Using 'this' in Initializer List?
    By Tonto in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2006, 01:34 PM
  4. constructor's initializer list: order of evaluation
    By rpet in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 07:21 PM
  5. initializer list help
    By kashifk in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2003, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21