initializer_list for Vector Vs initializer_list for Map | Sololearn: Learn to code for FREE!


initializer_list for Vector Vs initializer_list for Map

Hi Please refer below code: Observe that initializer list for map and vector works without any error in main function. vector<int> v and map<char,int> m compiles without error. As per my understanding , these lines compiles because vector and map has compiler generated initializer_list constructor since C++11. Correct me if I am wrong. Now, go to constructor of class clsForVector. m_input = ls; compiles and works without error. where as m_input = ls; does not compile for constructor of class clsForMap. Why this behavior? Does this means C++11 has assignment operator overloaded for initializer list in case of vector and not for map... If not, what's the reason of compilation failure. If yes , why default assignment with initializer is not there for map?

4/15/2021 5:35:44 AM

Ketan Lalcheta

3 Answers

New Answer


You have to pass const char instead of char pair<const char, int>


I guess it is because of allocator, which takes "const Key" value


Wow. Great, it works.. Wondering why it works with const char?