diff --git a/xeus-cpp/02_persistence.ipynb b/xeus-cpp/02_persistence.ipynb index a208477..dd7c66e 100644 --- a/xeus-cpp/02_persistence.ipynb +++ b/xeus-cpp/02_persistence.ipynb @@ -69,6 +69,7 @@ }, { "cell_type": "code", + "execution_count": 4, "execution_count": 3, "id": "d2c38c70-3e92-446a-8c10-1c62edf177e1", "metadata": {}, @@ -77,6 +78,7 @@ "name": "stdout", "output_type": "stream", "text": [ + "Current Value: 2\n" "Current Value: 1\n" ] } @@ -84,6 +86,14 @@ "source": [ "inc(); //Run this cell multiple times to see persistence in action" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e4068173-ab1b-44e5-b439-a4063adfaa67", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -97,6 +107,7 @@ "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "C++", + "version": "23" "nbconvert_exporter": "", "pygments_lexer": "", "version": "cxx23" diff --git a/xeus-cpp/06_cpp_oop.ipynb b/xeus-cpp/06_cpp_oop.ipynb index 92d373c..e6acf72 100644 --- a/xeus-cpp/06_cpp_oop.ipynb +++ b/xeus-cpp/06_cpp_oop.ipynb @@ -286,6 +286,7 @@ "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "C++", + "version": "17" "nbconvert_exporter": "", "pygments_lexer": "", "version": "cxx17" diff --git a/xeus-cpp/07_stl_modern_cpp.ipynb b/xeus-cpp/07_stl_modern_cpp.ipynb index 62c51b0..3e032ef 100644 --- a/xeus-cpp/07_stl_modern_cpp.ipynb +++ b/xeus-cpp/07_stl_modern_cpp.ipynb @@ -41,6 +41,8 @@ "#include \n", "#include \n", "#include \n", + "#include \n", + "using namespace std;" "#include " ] }, @@ -69,6 +71,21 @@ } ], "source": [ + "vector v = {5, 2, 8, 1, 9, 3, 7, 4, 6};\n", + "\n", + "// Sort and display\n", + "sort(v.begin(), v.end());\n", + "cout << \"Sorted: \";\n", + "for (int x : v) cout << x << \" \";\n", + "cout << endl;\n", + "\n", + "// Accumulate\n", + "int sum = accumulate(v.begin(), v.end(), 0);\n", + "cout << \"Sum: \" << sum << \" Average: \" << (double)sum / v.size() << endl;\n", + "\n", + "// Find\n", + "auto it = find(v.begin(), v.end(), 7);\n", + "cout << \"Found 7 at index: \" << distance(v.begin(), it) << endl;" "std::vector v = {5, 2, 8, 1, 9, 3, 7, 4, 6};\n", "\n", "// Sort and display\n", @@ -102,6 +119,11 @@ ], "source": [ "// Transform: square each element\n", + "vector squares(v.size());\n", + "transform(v.begin(), v.end(), squares.begin(), [](int x) { return x * x; });\n", + "cout << \"Squares: \";\n", + "for (int x : squares) cout << x << \" \";\n", + "cout << endl;" "std::vector squares(v.size());\n", "std::transform(v.begin(), v.end(), squares.begin(), [](int x) { return x * x; });\n", "std::cout << \"Squares: \";\n", @@ -141,6 +163,16 @@ ], "source": [ "// Word frequency counter\n", + "string text = \"the quick brown fox jumps over the lazy dog\";\n", + "map freq;\n", + "\n", + "string word;\n", + "stringstream ss(text);\n", + "while (ss >> word) freq[word]++;\n", + "\n", + "cout << \"Word frequencies (alphabetical):\" << endl;\n", + "for (const auto& [w, cnt] : freq) {\n", + " cout << \" \" << w << \": \" << cnt << endl;\n", "std::string text = \"the quick brown fox jumps over the lazy dog\";\n", "std::map freq;\n", "\n", @@ -171,6 +203,14 @@ ], "source": [ "// Set: unique sorted elements\n", + "vector dupes = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};\n", + "set unique_set(dupes.begin(), dupes.end());\n", + "\n", + "cout << \"Original (\" << dupes.size() << \" elements): \";\n", + "for (int x : dupes) cout << x << \" \";\n", + "cout << \"\\nUnique (\" << unique_set.size() << \" elements): \";\n", + "for (int x : unique_set) cout << x << \" \";\n", + "cout << endl;" "std::vector dupes = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};\n", "std::set unique_set(dupes.begin(), dupes.end());\n", "\n", @@ -211,6 +251,17 @@ "auto isAbove = [threshold](int x) { return x > threshold; }; // capture by value\n", "auto addTo = [&threshold](int x) { return x + threshold; }; // capture by reference\n", "\n", + "vector nums = {1, 3, 5, 7, 9, 2, 4, 6, 8};\n", + "vector above, shifted;\n", + "\n", + "copy_if(nums.begin(), nums.end(), back_inserter(above), isAbove);\n", + "transform(nums.begin(), nums.end(), back_inserter(shifted), addTo);\n", + "\n", + "cout << \"Above \" << threshold << \": \";\n", + "for (int x : above) cout << x << \" \";\n", + "cout << \"\\nShifted +\" << threshold << \": \";\n", + "for (int x : shifted) cout << x << \" \";\n", + "cout << endl;\n", "std::vector nums = {1, 3, 5, 7, 9, 2, 4, 6, 8};\n", "std::vector above, shifted;\n", "\n", @@ -253,6 +304,12 @@ "source": [ "// unique_ptr — single ownership\n", "{\n", + " auto uptr = make_unique(\"unique owner\");\n", + " cout << \"unique_ptr: \" << *uptr << endl;\n", + " // auto uptr2 = uptr; // ERROR: cannot copy unique_ptr\n", + " auto uptr2 = move(uptr); // Transfer ownership\n", + " cout << \"After move, uptr is \" << (uptr ? \"valid\" : \"null\") << endl;\n", + " cout << \"uptr2: \" << *uptr2 << endl;\n", " auto uptr = std::make_unique(\"unique owner\");\n", " std::cout << \"unique_ptr: \" << *uptr << std::endl;\n", " // auto uptr2 = uptr; // ERROR: cannot copy unique_ptr\n", @@ -263,6 +320,9 @@ "\n", "// shared_ptr — shared ownership\n", "{\n", + " auto sptr1 = make_shared(42);\n", + " auto sptr2 = sptr1; // Both share ownership\n", + " cout << \"shared value: \" << *sptr1 << \" use_count: \" << sptr1.use_count() << endl;\n", " auto sptr1 = std::make_shared(42);\n", " auto sptr2 = sptr1; // Both share ownership\n", " std::cout << \"shared value: \" << *sptr1 << \" use_count: \" << sptr1.use_count() << std::endl;\n", @@ -294,6 +354,8 @@ ], "source": [ "// std::optional\n", + "optional safeDivide(int a, int b) {\n", + " if (b == 0) return nullopt;\n", "std::optional safeDivide(int a, int b) {\n", " if (b == 0) return std::nullopt;\n", " return a / b;\n", @@ -302,12 +364,28 @@ "auto r1 = safeDivide(10, 2);\n", "auto r2 = safeDivide(10, 0);\n", "\n", + "cout << \"10/2 = \" << (r1 ? to_string(*r1) : \"undefined\") << endl;\n", + "cout << \"10/0 = \" << (r2 ? to_string(*r2) : \"undefined\") << endl;" "std::cout << \"10/2 = \" << (r1 ? std::to_string(*r1) : \"undefined\") << std::endl;\n", "std::cout << \"10/0 = \" << (r2 ? std::to_string(*r2) : \"undefined\") << std::endl;" ] }, { "cell_type": "code", + "execution_count": null, + "id": "variant-demo", + "metadata": {}, + "outputs": [], + "source": [ + "// std::variant\n", + "using Number = variant;\n", + "\n", + "vector numbers = {42, 3.14, string(\"forty-two\"), 100, 2.718};\n", + "\n", + "for (const auto& n : numbers) {\n", + " visit([](const auto& val) {\n", + " cout << val << \" (\" << typeid(val).name() << \")\" << endl;\n", + " }, n);\n", "execution_count": 9, "id": "variant-demo", "metadata": {}, @@ -353,6 +431,9 @@ ], "metadata": { "kernelspec": { + "display_name": "C++17", + "language": "cpp", + "name": "xcpp17" "display_name": "C++23", "language": "cpp", "name": "xcpp23" @@ -362,6 +443,7 @@ "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "C++", + "version": "17" "nbconvert_exporter": "", "pygments_lexer": "", "version": "cxx23"