add list-initializers.cpp

master
neingeist 11 years ago
parent 8f9dd5cac6
commit 11ece493be

1
.gitignore vendored

@ -8,3 +8,4 @@ Makefile
auto_ptr
typetest
unique_ptr
list-initializers

@ -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)

@ -0,0 +1,31 @@
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <utility>
using namespace std;
int main() {
// notice how the lists are nested to match the templates' parameters:
map<string, vector<pair<string, int>>> 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;
}
Loading…
Cancel
Save