Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions xeus-cpp/02_persistence.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "d2c38c70-3e92-446a-8c10-1c62edf177e1",
"metadata": {},
Expand All @@ -77,13 +78,22 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Current Value: 2\n"
"Current Value: 1\n"
]
}
],
"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": {
Expand All @@ -97,6 +107,7 @@
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "C++",
"version": "23"
"nbconvert_exporter": "",
"pygments_lexer": "",
"version": "cxx23"
Expand Down
1 change: 1 addition & 0 deletions xeus-cpp/06_cpp_oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "C++",
"version": "17"
"nbconvert_exporter": "",
"pygments_lexer": "",
"version": "cxx17"
Expand Down
82 changes: 82 additions & 0 deletions xeus-cpp/07_stl_modern_cpp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"#include <optional>\n",
"#include <variant>\n",
"#include <functional>\n",
"#include <sstream>\n",
"using namespace std;"
"#include <sstream>"
]
},
Expand Down Expand Up @@ -69,6 +71,21 @@
}
],
"source": [
"vector<int> 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<int> v = {5, 2, 8, 1, 9, 3, 7, 4, 6};\n",
"\n",
"// Sort and display\n",
Expand Down Expand Up @@ -102,6 +119,11 @@
],
"source": [
"// Transform: square each element\n",
"vector<int> 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<int> squares(v.size());\n",
"std::transform(v.begin(), v.end(), squares.begin(), [](int x) { return x * x; });\n",
"std::cout << \"Squares: \";\n",
Expand Down Expand Up @@ -141,6 +163,16 @@
],
"source": [
"// Word frequency counter\n",
"string text = \"the quick brown fox jumps over the lazy dog\";\n",
"map<string, int> 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<std::string, int> freq;\n",
"\n",
Expand Down Expand Up @@ -171,6 +203,14 @@
],
"source": [
"// Set: unique sorted elements\n",
"vector<int> dupes = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};\n",
"set<int> 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<int> dupes = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};\n",
"std::set<int> unique_set(dupes.begin(), dupes.end());\n",
"\n",
Expand Down Expand Up @@ -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<int> nums = {1, 3, 5, 7, 9, 2, 4, 6, 8};\n",
"vector<int> 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<int> nums = {1, 3, 5, 7, 9, 2, 4, 6, 8};\n",
"std::vector<int> above, shifted;\n",
"\n",
Expand Down Expand Up @@ -253,6 +304,12 @@
"source": [
"// unique_ptr — single ownership\n",
"{\n",
" auto uptr = make_unique<string>(\"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<std::string>(\"unique owner\");\n",
" std::cout << \"unique_ptr: \" << *uptr << std::endl;\n",
" // auto uptr2 = uptr; // ERROR: cannot copy unique_ptr\n",
Expand All @@ -263,6 +320,9 @@
"\n",
"// shared_ptr — shared ownership\n",
"{\n",
" auto sptr1 = make_shared<int>(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<int>(42);\n",
" auto sptr2 = sptr1; // Both share ownership\n",
" std::cout << \"shared value: \" << *sptr1 << \" use_count: \" << sptr1.use_count() << std::endl;\n",
Expand Down Expand Up @@ -294,6 +354,8 @@
],
"source": [
"// std::optional\n",
"optional<int> safeDivide(int a, int b) {\n",
" if (b == 0) return nullopt;\n",
"std::optional<int> safeDivide(int a, int b) {\n",
" if (b == 0) return std::nullopt;\n",
" return a / b;\n",
Expand All @@ -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<int, double, string>;\n",
"\n",
"vector<Number> 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": {},
Expand Down Expand Up @@ -353,6 +431,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "cpp",
"name": "xcpp17"
"display_name": "C++23",
"language": "cpp",
"name": "xcpp23"
Expand All @@ -362,6 +443,7 @@
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "C++",
"version": "17"
"nbconvert_exporter": "",
"pygments_lexer": "",
"version": "cxx23"
Expand Down