|
1 | 1 | [
|
| 2 | + { |
| 3 | + "categoryName": "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 | "categoryName": "Basics",
|
4 | 33 | "snippets": [
|
|
32 | 61 | }
|
33 | 62 | ]
|
34 | 63 | },
|
| 64 | + { |
| 65 | + "categoryName": "File Handling", |
| 66 | + "snippets": [ |
| 67 | + { |
| 68 | + "title": "Find files recursively", |
| 69 | + "description": "Find all the files in a directory and subdirectories using a predicate function.", |
| 70 | + "author": "majvax", |
| 71 | + "tags": [ |
| 72 | + "filesystem", |
| 73 | + "file_search", |
| 74 | + "c++17" |
| 75 | + ], |
| 76 | + "contributors": [], |
| 77 | + "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" |
| 78 | + }, |
| 79 | + { |
| 80 | + "title": "Find files", |
| 81 | + "description": "Find all the files in a directory using a predicate function.", |
| 82 | + "author": "majvax", |
| 83 | + "tags": [ |
| 84 | + "filesystem", |
| 85 | + "file_search", |
| 86 | + "c++17" |
| 87 | + ], |
| 88 | + "contributors": [], |
| 89 | + "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" |
| 90 | + }, |
| 91 | + { |
| 92 | + "title": "List Directories", |
| 93 | + "description": "Lists all the directories in a path.", |
| 94 | + "author": "majvax", |
| 95 | + "tags": [ |
| 96 | + "filesystem", |
| 97 | + "directories", |
| 98 | + "c++17" |
| 99 | + ], |
| 100 | + "contributors": [], |
| 101 | + "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" |
| 102 | + } |
| 103 | + ] |
| 104 | + }, |
35 | 105 | {
|
36 | 106 | "categoryName": "Math And Numbers",
|
37 | 107 | "snippets": [
|
|
51 | 121 | {
|
52 | 122 | "categoryName": "String Manipulation",
|
53 | 123 | "snippets": [
|
| 124 | + { |
| 125 | + "title": "Filter", |
| 126 | + "description": "Filter a string with a predicate function", |
| 127 | + "author": "majvax", |
| 128 | + "tags": [ |
| 129 | + "string", |
| 130 | + "filtering", |
| 131 | + "c++23" |
| 132 | + ], |
| 133 | + "contributors": [], |
| 134 | + "code": "#include <ranges>\n#include <string>\n\ntemplate <typename P>\nstd::string filter(const std::string& str, P&& predicate) {\n return str\n | std::ranges::views::filter(std::forward<P>(predicate))\n | std::ranges::to<std::string>();\n}\n\n\n\n// Usage:\nstd::string str = \"Hello, World!\";\nstd::string filtered = filter(str, [](char c){ return std::isalpha(c); });\nstd::cout << filtered << std::endl; // HelloWorld\n" |
| 135 | + }, |
| 136 | + { |
| 137 | + "title": "Palindrome", |
| 138 | + "description": "Check if a string is a palindrome or not.", |
| 139 | + "author": "majvax", |
| 140 | + "tags": [ |
| 141 | + "string", |
| 142 | + "c++23" |
| 143 | + ], |
| 144 | + "contributors": [], |
| 145 | + "code": "#include <string>\n#include <ranges>\n#include <algorithm>\n\nbool is_palindrome(const std::string& str) {\n std::string sanitized_string = str\n | std::ranges::views::filter([](char c){ return std::isalnum(c); })\n | std::ranges::views::transform([](char c){ return std::tolower(c); })\n | std::ranges::to<std::string>();\n \n return std::ranges::equal(sanitized_string, sanitized_string | std::views::reverse);\n}\n\n\n\n// Usage:\nbool pal = is_palindrome(\"A man, a plan, a canal, Panama\"); // true\n" |
| 146 | + }, |
54 | 147 | {
|
55 | 148 | "title": "Reverse String",
|
56 | 149 | "description": "Reverses the characters in a string.",
|
57 | 150 | "author": "Vaibhav-kesarwani",
|
58 | 151 | "tags": [
|
59 | 152 | "array",
|
60 |
| - "reverse" |
| 153 | + "reverse", |
| 154 | + "c++23" |
61 | 155 | ],
|
62 | 156 | "contributors": [],
|
63 | 157 | "code": "#include <string>\n#include <algorithm>\n\nstd::string reverseString(const std::string& input) {\n std::string reversed = input;\n std::reverse(reversed.begin(), reversed.end());\n return reversed;\n}\n\nreverseString(\"quicksnip\"); // Returns: \"pinskciuq\"\n"
|
|
72 | 166 | ],
|
73 | 167 | "contributors": [],
|
74 | 168 | "code": "#include <string>\n#include <vector>\n\nstd::vector<std::string> split_string(std::string str, std::string delim) {\n std::vector<std::string> splits;\n int i = 0, j;\n int inc = delim.length();\n while (j != std::string::npos) {\n j = str.find(delim, i);\n splits.push_back(str.substr(i, j - i));\n i = j + inc;\n }\n return splits;\n}\n\n// Usage:\nsplit_string(\"quick_-snip\", \"_-\"); // Returns: std::vector<std::string> { \"quick\", \"snip\" }\n"
|
| 169 | + }, |
| 170 | + { |
| 171 | + "title": "Transform", |
| 172 | + "description": "Transform a string with a function", |
| 173 | + "author": "majvax", |
| 174 | + "tags": [ |
| 175 | + "string", |
| 176 | + "transform", |
| 177 | + "c++23" |
| 178 | + ], |
| 179 | + "contributors": [], |
| 180 | + "code": "#include <ranges>\n#include <string>\n\ntemplate <typename F>\nstd::string transform(const std::string& str, F&& transformer) {\n return str\n | std::ranges::views::transform(std::forward<F>(transformer))\n | std::ranges::to<std::string>();\n}\n\n\n\n// Usage:\nstd::string str = \"Hello, World!\";\nstd::string transformed = transform(str, [](char c){ return std::toupper(c); });\nstd::cout << transformed << std::endl; // HELLO, WORLD!\n" |
75 | 181 | }
|
76 | 182 | ]
|
77 | 183 | }
|
|
0 commit comments