|
1 | 1 | [
|
| 2 | + { |
| 3 | + "name": "Array Manipulation", |
| 4 | + "snippets": [ |
| 5 | + { |
| 6 | + "title": "Filter Vector", |
| 7 | + "description": "Filters a vector using a predicate function.", |
| 8 | + "author": "majvax", |
| 9 | + "tags": [ |
| 10 | + "array", |
| 11 | + "filter", |
| 12 | + "c++23" |
| 13 | + ], |
| 14 | + "contributors": [], |
| 15 | + "code": "#include <ranges>\n#include <vector>\n\ntemplate <typename T, typename P>\nauto filter(const std::vector<T>& vec, P&& predicate) {\n return vec\n | std::views::filter(std::forward<P>(predicate))\n | std::ranges::to<std::vector<T>>();\n}\n\n\n\n// Usage:\nstd::vector<int> vec = {1, 2, 3, 4, 5};\nstd::vector<int> filtered = filter(vec, [](int i){ return i % 2 == 0; });\n// filtered contains 2 and 4\n" |
| 16 | + }, |
| 17 | + { |
| 18 | + "title": "Transform Vector", |
| 19 | + "description": "Transforms a vector using a function.", |
| 20 | + "author": "majvax", |
| 21 | + "tags": [ |
| 22 | + "array", |
| 23 | + "transform", |
| 24 | + "c++23" |
| 25 | + ], |
| 26 | + "contributors": [], |
| 27 | + "code": "#include <ranges>\n#include <vector>\n\ntemplate <typename T, typename F>\nauto transform(const std::vector<T>& vec, F&& transformer) {\n using U = std::invoke_result_t<F, T>;\n return vec\n | std::views::transform(std::forward<F>(transformer))\n | std::ranges::to<std::vector<U>>();\n}\n\n\n\n// Usage:\nstd::vector<int> vec = {1, 2, 3, 4, 5};\nstd::vector<int> transformed = transform(vec, [](int i){ return i * 2; });\n// transformed contains 2, 4, 6, 8, 10\n" |
| 28 | + } |
| 29 | + ] |
| 30 | + }, |
2 | 31 | {
|
3 | 32 | "name": "Basics",
|
4 | 33 | "snippets": [
|
|
66 | 95 | }
|
67 | 96 | ]
|
68 | 97 | },
|
| 98 | + { |
| 99 | + "name": "File Handling", |
| 100 | + "snippets": [ |
| 101 | + { |
| 102 | + "title": "Find files recursively", |
| 103 | + "description": "Find all the files in a directory and subdirectories using a predicate function.", |
| 104 | + "author": "majvax", |
| 105 | + "tags": [ |
| 106 | + "filesystem", |
| 107 | + "file_search", |
| 108 | + "c++17" |
| 109 | + ], |
| 110 | + "contributors": [], |
| 111 | + "code": "#include <filesystem>\n#include <vector>\n#include <string>\n\ntemplate <typename P>\nstd::vector<std::filesystem::path> find_files_recursive(const std::string& path, P&& predicate) {\n std::vector<std::filesystem::path> files;\n std::error_code ec;\n\n if (!std::filesystem::exists(path, ec) || ec)\n return files;\n if (!std::filesystem::is_directory(path, ec) || ec)\n return files;\n\n auto it = std::filesystem::recursive_directory_iterator(path, ec);\n if (ec)\n return files;\n\n for (const auto& entry : it)\n if (!std::filesystem::is_directory(entry) && predicate(entry.path()))\n files.push_back(entry.path());\n\n return files;\n}\n\n\n\n// Usage:\n\n// Find all files with size greater than 10MB\nauto files = find_files_recursive(\"Path\", [](const auto& p) {\n return std::filesystem::file_size(p) > 10 * 1024 * 1024;\n});\n\n// Find all files with \".pdf\" as extension\nauto files = find_files_recursive(\"Path\", [](const auto& p) {\n return p.extension() == \".pdf\";\n});\n\n// Find all files writed after The New Year\n#include <chrono>\n// need std=c++20\nauto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys(\n std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}}\n);\nauto files = find_files_recursive(\"Path\", [jan_1_2025](const auto& p) {\n return std::filesystem::last_write_time(p) > jan_1_2025;\n}),\n" |
| 112 | + }, |
| 113 | + { |
| 114 | + "title": "Find files", |
| 115 | + "description": "Find all the files in a directory using a predicate function.", |
| 116 | + "author": "majvax", |
| 117 | + "tags": [ |
| 118 | + "filesystem", |
| 119 | + "file_search", |
| 120 | + "c++17" |
| 121 | + ], |
| 122 | + "contributors": [], |
| 123 | + "code": "#include <filesystem>\n#include <vector>\n#include <string>\n\ntemplate <typename P>\nstd::vector<std::filesystem::path> find_files(const std::string& path, P&& predicate) {\n std::vector<std::filesystem::path> files;\n std::error_code ec;\n\n if (!std::filesystem::exists(path, ec) || ec)\n return files;\n if (!std::filesystem::is_directory(path, ec) || ec)\n return files;\n\n auto it = std::filesystem::directory_iterator(path, ec);\n if (ec)\n return files;\n\n for (const auto& entry : it)\n if (!std::filesystem::is_directory(entry) && predicate(entry.path()))\n files.push_back(entry.path());\n\n return files;\n}\n\n\n\n// Usage:\n\n// Find all files with size greater than 10MB\nauto files = find_files(\"Path\", [](const auto& p) {\n return std::filesystem::file_size(p) > 10 * 1024 * 1024;\n});\n\n// Find all files with \".pdf\" as extension\nauto files = find_files(\"Path\", [](const auto& p) {\n return p.extension() == \".pdf\";\n});\n\n// Find all files writed after The New Year\n#include <chrono>\n// need std=c++20\nauto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys(\n std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}}\n);\nauto files = find_files(\"Path\", [jan_1_2025](const auto& p) {\n return std::filesystem::last_write_time(p) > jan_1_2025;\n}),\n" |
| 124 | + }, |
| 125 | + { |
| 126 | + "title": "List Directories", |
| 127 | + "description": "Lists all the directories in a path.", |
| 128 | + "author": "majvax", |
| 129 | + "tags": [ |
| 130 | + "filesystem", |
| 131 | + "directories", |
| 132 | + "c++17" |
| 133 | + ], |
| 134 | + "contributors": [], |
| 135 | + "code": "#include <filesystem>\n#include <vector>\n#include <string>\n\nstd::vector<std::filesystem::path> list_directories(const std::string& path) {\n std::vector<std::filesystem::path> files;\n std::error_code ec;\n\n if (!std::filesystem::exists(path, ec) || ec)\n return files;\n if (!std::filesystem::is_directory(path, ec) || ec)\n return files;\n\n auto it = std::filesystem::directory_iterator(path, ec);\n if (ec)\n return files;\n\n for (const auto& entry : it)\n if (std::filesystem::is_directory(entry))\n files.push_back(entry.path());\n\n return files;\n}\n\n\n\n// Usage:\nauto directories = list_directories(\"Path\");\n" |
| 136 | + } |
| 137 | + ] |
| 138 | + }, |
69 | 139 | {
|
70 | 140 | "name": "Math And Numbers",
|
71 | 141 | "snippets": [
|
|
0 commit comments