{"pageProps":{"code":{"CMakeLists.txt":{"name":"CMakeLists.txt","content":"add_executable(map-container map-container.cpp)\r\n\r\nadd_executable(vector-iterate vector-iterate.cpp)\r\n\r\nadd_executable(vector-object vector-object.cpp)\r\n","path":"code/6771/25T2/2.1/CMakeLists.txt","fileext":"txt"},"map-container.cpp":{"name":"map-container.cpp","content":"#include <iostream>\r\n#include <map>\r\n#include <string>\r\n\r\nint main()\r\n{\r\n    std::map<std::string, double> m;\r\n    // The insert function takes in a key-value pair.\r\n    std::pair<std::string, double> p1 { \"bat\", 14.75 };\r\n    m.insert(p1);\r\n    // The compiler will automatically construct values as\r\n    // required when it knows the required type.\r\n    m.insert({ \"cat\", 10.157 });\r\n    // This is the preferred way of using a map\r\n    m.emplace(\"cat\", 10.157);\r\n\r\n    // This is very dangerous, and one of the most common causes of mistakes in C++.\r\n    std::cout << m[\"bat\"] << '\\n';\r\n\r\n    auto it = m.find(\"bat\"); // Iterator to bat if present, otherwise m.end()\r\n    (void)it;\r\n\r\n    // This is a great example of when to use auto, but we want to show you what type it is.\r\n    for (const std::pair<const std::string, double>& kv : m) {\r\n        std::cout << kv.first << ' ' << kv.second << '\\n';\r\n    }\r\n}","path":"code/6771/25T2/2.1/map-container.cpp","fileext":"cpp"},"vector-iterate.cpp":{"name":"vector-iterate.cpp","content":"#include <array>\r\n#include <iostream>\r\n\r\nint main()\r\n{\r\n    // C-style. Don't do this\r\n    // int ages[3] = { 18, 19, 20 };\r\n    // for (int i = 0; i < 3; ++i) {\r\n    //   std::cout << ages[i] << \"\\n\";\r\n    // }\r\n\r\n    // C++ style. This can be used like any other C++ container.\r\n    // It has iterators, safe accesses, and it doesn't act like a pointer.\r\n    std::array<int, 3> ages { 18, 19, 20 };\r\n\r\n    for (unsigned int i = 0; i < ages.size(); ++i) {\r\n        std::cout << ages[i] << \"\\n\";\r\n    }\r\n    for (auto it = ages.begin(); it != ages.end(); ++it) {\r\n        std::cout << *it << \"\\n\";\r\n    }\r\n    for (const auto& age : ages) {\r\n        std::cout << age << \"\\n\";\r\n    }\r\n}","path":"code/6771/25T2/2.1/vector-iterate.cpp","fileext":"cpp"},"vector-object.cpp":{"name":"vector-object.cpp","content":"#include <iostream>\r\n#include <vector>\r\n\r\n// Begin with numbers 1, 2, 3 in the list already\r\nint main()\r\n{\r\n    // In C++17 we can omit the int if the compiler can determine the type.\r\n    std::vector<int> numbers { 1, 2, 3 };\r\n    int input;\r\n    while (std::cin >> input) {\r\n        numbers.push_back(input);\r\n    }\r\n    std::cout << \"1st element: \" << numbers.at(0) << \"\\n\"; // slower, safer\r\n    std::cout << \"2nd element: \" << numbers[1] << \"\\n\"; // faster, less safe\r\n    std::cout << \"Max size before realloc: \" << numbers.capacity() << \"\\n\";\r\n    for (int n : numbers) {\r\n        std::cout << n << \"\\n\";\r\n    }\r\n}","path":"code/6771/25T2/2.1/vector-object.cpp","fileext":"cpp"}}},"__N_SSG":true}