initializer_list for Vector Vs initializer_list for Map | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

initializer_list for Vector Vs initializer_list for Map

Hi Please refer below code: https://code.sololearn.com/c7A17a11A0a3 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?

15th Apr 2021, 5:35 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 1
You have to pass const char instead of char pair<const char, int>
15th Apr 2021, 6:13 AM
Michal Doruch
+ 1
I guess it is because of allocator, which takes "const Key" value
15th Apr 2021, 10:21 AM
Michal Doruch
0
Wow. Great, it works.. Wondering why it works with const char?
15th Apr 2021, 7:51 AM
Ketan Lalcheta
Ketan Lalcheta - avatar