0

I am a C programmer and new to C++. I have used maps before and insertion often was like map.insert(std::make_pair(key, value)). Today I saw in someone's code and further on this website a simpler syntax:

map.insert({key, value})

The linked website says {k,v} is a "pair" but does not explain how. This would not have been a surprise in Javascript where braces denote objects and everything is dynamic. But when did C++ evolve to support such features.

I am looking for an explanation in terms of static typing and type coercion, how {k,v} is same as std::pair

Edit: This question has been marked as duplicate of this one but the answers to that question deal entirely with what is faster by checking assembly code. The answer I was looking for has been given by user spectras in comments as list initialization. Before anyone rushes to mark a question as duplicate, do check whether answers to the linked question answer what was asked.

Siddhesh Rane
  • 419
  • 3
  • 7
  • Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In particular you should pick a book that covers C++11 or higher as modern C++ is a different language (not really but it makes life so much better) than C++98/03 – NathanOliver Jul 09 '19 at 14:54
  • Sounds like you need a modern C++ book. – Lightness Races in Orbit Jul 09 '19 at 14:54
  • 2
    https://en.cppreference.com/w/cpp/language/list_initialization – spectras Jul 09 '19 at 14:55
  • see http://www.cplusplus.com/reference/map/map/ – alon Jul 09 '19 at 14:55
  • I'm surprised, but I can't find any good question on basics of Uniform Initialization in C++. [This one](https://stackoverflow.com/questions/24720028/whats-the-difference-between-object-objargs-and-object-objargs?noredirect=1&lq=1) might be the closest to explaining the issue. – Yksisarvinen Jul 09 '19 at 14:59
  • This question has been marked as duplicate of [this one](https://stackoverflow.com/questions/52236209/difference-between-make-pair-and-curly-brackets-for-assigning-a-pair-in-c) but the answers to that question deal entirely with what is faster by checking assembly code. The answer I was looking for has been given by user spectras above as list initialization. Before anyone rushes to mark a question as duplicate, do check whether answers to the linked question answer what was asked. – Siddhesh Rane Jul 09 '19 at 15:29

0 Answers0