Thread: std::map initializer list syntax ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011

    std::map initializer list syntax ?

    C++11 supports the initialization of most containers by the simple syntax as follows:
    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 ?

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

    #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, 02:10 PM
  2. 3D array initializer list
    By kenryuakuma in forum C# Programming
    Replies: 5
    Last Post: 11-15-2009, 12:40 AM
  3. Using 'this' in Initializer List?
    By Tonto in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2006, 12:34 PM
  4. constructor's initializer list: order of evaluation
    By rpet in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 06:21 PM
  5. initializer list help
    By kashifk in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2003, 12:05 PM
Website Security Test