From 11ece493be7314930b5b9d08368c279fd31c89b2 Mon Sep 17 00:00:00 2001 From: neingeist Date: Sun, 13 Apr 2014 09:10:08 +0200 Subject: [PATCH] add list-initializers.cpp --- .gitignore | 1 + CMakeLists.txt | 1 + list-initializers.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 list-initializers.cpp diff --git a/.gitignore b/.gitignore index a8d65df..0b85be5 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ Makefile auto_ptr typetest unique_ptr +list-initializers diff --git a/CMakeLists.txt b/CMakeLists.txt index d910bfe..6cd51dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,3 +6,4 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -Weffc++") add_executable(typetest typetest.cpp) add_executable(auto_ptr auto_ptr.cpp) add_executable(unique_ptr unique_ptr.cpp) +add_executable(list-initializers list-initializers.cpp) diff --git a/list-initializers.cpp b/list-initializers.cpp new file mode 100644 index 0000000..8475b68 --- /dev/null +++ b/list-initializers.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + // notice how the lists are nested to match the templates' parameters: + map>> name_languages_year { + {"Dennis Ritchie", {{"B", 1969}, {"C", 1973}}}, + {"Niklaus Wirth", {{"Pascal", 1970}, {"Modula-2", 1973}, + {"Oberon", 1986}}}, + {"Bjarne Stroustrup", {{"C++", 1983}}}, + {"Walter Bright", {{"D", 1999}}} + }; + + // prints `Pascal': + cout << name_languages_year["Niklaus Wirth"].at(0).first << endl; + + // adds a new entry to the map: + name_languages_year["John McCarthy"] = { + {"Lisp", 1958} + }; + + // prints 'Lisp': + cout << name_languages_year["John McCarthy"].at(0).first << endl; + +}