diff --git a/public/consolidated/cpp.json b/public/consolidated/cpp.json index c121a725..a2ac2902 100644 --- a/public/consolidated/cpp.json +++ b/public/consolidated/cpp.json @@ -1,4 +1,33 @@ [ + { + "name": "Array Manipulation", + "snippets": [ + { + "title": "Filter Vector", + "description": "Filters a vector using a predicate function.", + "author": "majvax", + "tags": [ + "array", + "filter", + "c++23" + ], + "contributors": [], + "code": "#include \n#include \n\ntemplate \nauto filter(const std::vector& vec, P&& predicate) {\n return vec\n | std::views::filter(std::forward

(predicate))\n | std::ranges::to>();\n}\n\n\n\n// Usage:\nstd::vector vec = {1, 2, 3, 4, 5};\nstd::vector filtered = filter(vec, [](int i){ return i % 2 == 0; });\n// filtered contains 2 and 4\n" + }, + { + "title": "Transform Vector", + "description": "Transforms a vector using a function.", + "author": "majvax", + "tags": [ + "array", + "transform", + "c++23" + ], + "contributors": [], + "code": "#include \n#include \n\ntemplate \nauto transform(const std::vector& vec, F&& transformer) {\n using U = std::invoke_result_t;\n return vec\n | std::views::transform(std::forward(transformer))\n | std::ranges::to>();\n}\n\n\n\n// Usage:\nstd::vector vec = {1, 2, 3, 4, 5};\nstd::vector transformed = transform(vec, [](int i){ return i * 2; });\n// transformed contains 2, 4, 6, 8, 10\n" + } + ] + }, { "name": "Basics", "snippets": [ @@ -66,6 +95,47 @@ } ] }, + { + "name": "File Handling", + "snippets": [ + { + "title": "Find files recursively", + "description": "Find all the files in a directory and subdirectories using a predicate function.", + "author": "majvax", + "tags": [ + "filesystem", + "file_search", + "c++17" + ], + "contributors": [], + "code": "#include \n#include \n#include \n\ntemplate \nstd::vector find_files_recursive(const std::string& path, P&& predicate) {\n std::vector 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 \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" + }, + { + "title": "Find files", + "description": "Find all the files in a directory using a predicate function.", + "author": "majvax", + "tags": [ + "filesystem", + "file_search", + "c++17" + ], + "contributors": [], + "code": "#include \n#include \n#include \n\ntemplate \nstd::vector find_files(const std::string& path, P&& predicate) {\n std::vector 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 \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" + }, + { + "title": "List Directories", + "description": "Lists all the directories in a path.", + "author": "majvax", + "tags": [ + "filesystem", + "directories", + "c++17" + ], + "contributors": [], + "code": "#include \n#include \n#include \n\nstd::vector list_directories(const std::string& path) {\n std::vector 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" + } + ] + }, { "name": "Math And Numbers", "snippets": [ @@ -85,13 +155,37 @@ { "name": "String Manipulation", "snippets": [ + { + "title": "Filter", + "description": "Filter a string with a predicate function", + "author": "majvax", + "tags": [ + "string", + "filtering", + "c++23" + ], + "contributors": [], + "code": "#include \n#include \n\ntemplate \nstd::string filter(const std::string& str, P&& predicate) {\n return str\n | std::ranges::views::filter(std::forward

(predicate))\n | std::ranges::to();\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" + }, + { + "title": "Palindrome", + "description": "Check if a string is a palindrome or not.", + "author": "majvax", + "tags": [ + "string", + "c++23" + ], + "contributors": [], + "code": "#include \n#include \n#include \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();\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" + }, { "title": "Reverse String", "description": "Reverses the characters in a string.", "author": "Vaibhav-kesarwani", "tags": [ "array", - "reverse" + "reverse", + "c++23" ], "contributors": [], "code": "#include \n#include \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" @@ -106,6 +200,18 @@ ], "contributors": [], "code": "#include \n#include \n\nstd::vector split_string(std::string str, std::string delim) {\n std::vector 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 { \"quick\", \"snip\" }\n" + }, + { + "title": "Transform", + "description": "Transform a string with a function", + "author": "majvax", + "tags": [ + "string", + "transform", + "c++23" + ], + "contributors": [], + "code": "#include \n#include \n\ntemplate \nstd::string transform(const std::string& str, F&& transformer) {\n return str\n | std::ranges::views::transform(std::forward(transformer))\n | std::ranges::to();\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" } ] } diff --git a/snippets/cpp/array-manipulation/filter-vector.md b/snippets/cpp/array-manipulation/filter-vector.md new file mode 100644 index 00000000..083f7899 --- /dev/null +++ b/snippets/cpp/array-manipulation/filter-vector.md @@ -0,0 +1,25 @@ +--- +Title: Filter Vector +Description: Filters a vector using a predicate function. +Author: majvax +Tags: array,filter,c++23 +--- + +```cpp +#include +#include + +template +auto filter(const std::vector& vec, P&& predicate) { + return vec + | std::views::filter(std::forward

(predicate)) + | std::ranges::to>(); +} + + + +// Usage: +std::vector vec = {1, 2, 3, 4, 5}; +std::vector filtered = filter(vec, [](int i){ return i % 2 == 0; }); +// filtered contains 2 and 4 +``` diff --git a/snippets/cpp/array-manipulation/transform-vector.md b/snippets/cpp/array-manipulation/transform-vector.md new file mode 100644 index 00000000..e01cbabe --- /dev/null +++ b/snippets/cpp/array-manipulation/transform-vector.md @@ -0,0 +1,26 @@ +--- +Title: Transform Vector +Description: Transforms a vector using a function. +Author: majvax +Tags: array,transform,c++23 +--- + +```cpp +#include +#include + +template +auto transform(const std::vector& vec, F&& transformer) { + using U = std::invoke_result_t; + return vec + | std::views::transform(std::forward(transformer)) + | std::ranges::to>(); +} + + + +// Usage: +std::vector vec = {1, 2, 3, 4, 5}; +std::vector transformed = transform(vec, [](int i){ return i * 2; }); +// transformed contains 2, 4, 6, 8, 10 +``` diff --git a/snippets/cpp/file-handling/find-files-recursively.md b/snippets/cpp/file-handling/find-files-recursively.md new file mode 100644 index 00000000..83d974e4 --- /dev/null +++ b/snippets/cpp/file-handling/find-files-recursively.md @@ -0,0 +1,57 @@ +--- +Title: Find files recursively +Description: Find all the files in a directory and subdirectories using a predicate function. +Author: majvax +Tags: filesystem,file_search,c++17 +--- + +```cpp +#include +#include +#include + +template +std::vector find_files_recursive(const std::string& path, P&& predicate) { + std::vector files; + std::error_code ec; + + if (!std::filesystem::exists(path, ec) || ec) + return files; + if (!std::filesystem::is_directory(path, ec) || ec) + return files; + + auto it = std::filesystem::recursive_directory_iterator(path, ec); + if (ec) + return files; + + for (const auto& entry : it) + if (!std::filesystem::is_directory(entry) && predicate(entry.path())) + files.push_back(entry.path()); + + return files; +} + + + +// Usage: + +// Find all files with size greater than 10MB +auto files = find_files_recursive("Path", [](const auto& p) { + return std::filesystem::file_size(p) > 10 * 1024 * 1024; +}); + +// Find all files with ".pdf" as extension +auto files = find_files_recursive("Path", [](const auto& p) { + return p.extension() == ".pdf"; +}); + +// Find all files writed after The New Year +#include +// need std=c++20 +auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys( + std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}} +); +auto files = find_files_recursive("Path", [jan_1_2025](const auto& p) { + return std::filesystem::last_write_time(p) > jan_1_2025; +}), +``` diff --git a/snippets/cpp/file-handling/find-files.md b/snippets/cpp/file-handling/find-files.md new file mode 100644 index 00000000..c0ff9331 --- /dev/null +++ b/snippets/cpp/file-handling/find-files.md @@ -0,0 +1,57 @@ +--- +Title: Find files +Description: Find all the files in a directory using a predicate function. +Author: majvax +Tags: filesystem,file_search,c++17 +--- + +```cpp +#include +#include +#include + +template +std::vector find_files(const std::string& path, P&& predicate) { + std::vector files; + std::error_code ec; + + if (!std::filesystem::exists(path, ec) || ec) + return files; + if (!std::filesystem::is_directory(path, ec) || ec) + return files; + + auto it = std::filesystem::directory_iterator(path, ec); + if (ec) + return files; + + for (const auto& entry : it) + if (!std::filesystem::is_directory(entry) && predicate(entry.path())) + files.push_back(entry.path()); + + return files; +} + + + +// Usage: + +// Find all files with size greater than 10MB +auto files = find_files("Path", [](const auto& p) { + return std::filesystem::file_size(p) > 10 * 1024 * 1024; +}); + +// Find all files with ".pdf" as extension +auto files = find_files("Path", [](const auto& p) { + return p.extension() == ".pdf"; +}); + +// Find all files writed after The New Year +#include +// need std=c++20 +auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys( + std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}} +); +auto files = find_files("Path", [jan_1_2025](const auto& p) { + return std::filesystem::last_write_time(p) > jan_1_2025; +}), +``` diff --git a/snippets/cpp/file-handling/list-directories.md b/snippets/cpp/file-handling/list-directories.md new file mode 100644 index 00000000..028bc095 --- /dev/null +++ b/snippets/cpp/file-handling/list-directories.md @@ -0,0 +1,37 @@ +--- +Title: List Directories +Description: Lists all the directories in a path. +Author: majvax +Tags: filesystem,directories,c++17 +--- + +```cpp +#include +#include +#include + +std::vector list_directories(const std::string& path) { + std::vector files; + std::error_code ec; + + if (!std::filesystem::exists(path, ec) || ec) + return files; + if (!std::filesystem::is_directory(path, ec) || ec) + return files; + + auto it = std::filesystem::directory_iterator(path, ec); + if (ec) + return files; + + for (const auto& entry : it) + if (std::filesystem::is_directory(entry)) + files.push_back(entry.path()); + + return files; +} + + + +// Usage: +auto directories = list_directories("Path"); +``` diff --git a/snippets/cpp/string-manipulation/filter.md b/snippets/cpp/string-manipulation/filter.md new file mode 100644 index 00000000..b09b50f5 --- /dev/null +++ b/snippets/cpp/string-manipulation/filter.md @@ -0,0 +1,25 @@ +--- +title: Filter +description: Filter a string with a predicate function +author: majvax +tags: string,filtering,c++23 +--- + +```cpp +#include +#include + +template +std::string filter(const std::string& str, P&& predicate) { + return str + | std::ranges::views::filter(std::forward

(predicate)) + | std::ranges::to(); +} + + + +// Usage: +std::string str = "Hello, World!"; +std::string filtered = filter(str, [](char c){ return std::isalpha(c); }); +std::cout << filtered << std::endl; // HelloWorld +``` diff --git a/snippets/cpp/string-manipulation/palindrome.md b/snippets/cpp/string-manipulation/palindrome.md new file mode 100644 index 00000000..b563e22c --- /dev/null +++ b/snippets/cpp/string-manipulation/palindrome.md @@ -0,0 +1,26 @@ +--- +title: Palindrome +description: Check if a string is a palindrome or not. +author: majvax +tags: string,c++23 +--- + +```cpp +#include +#include +#include + +bool is_palindrome(const std::string& str) { + std::string sanitized_string = str + | std::ranges::views::filter([](char c){ return std::isalnum(c); }) + | std::ranges::views::transform([](char c){ return std::tolower(c); }) + | std::ranges::to(); + + return std::ranges::equal(sanitized_string, sanitized_string | std::views::reverse); +} + + + +// Usage: +bool pal = is_palindrome("A man, a plan, a canal, Panama"); // true +``` diff --git a/snippets/cpp/string-manipulation/reverse-string.md b/snippets/cpp/string-manipulation/reverse-string.md index 615189b1..ed3c3995 100644 --- a/snippets/cpp/string-manipulation/reverse-string.md +++ b/snippets/cpp/string-manipulation/reverse-string.md @@ -2,7 +2,7 @@ title: Reverse String description: Reverses the characters in a string. author: Vaibhav-kesarwani -tags: array,reverse +tags: array,reverse,c++23 --- ```cpp diff --git a/snippets/cpp/string-manipulation/transform.md b/snippets/cpp/string-manipulation/transform.md new file mode 100644 index 00000000..3d0ee01d --- /dev/null +++ b/snippets/cpp/string-manipulation/transform.md @@ -0,0 +1,25 @@ +--- +title: Transform +description: Transform a string with a function +author: majvax +tags: string,transform,c++23 +--- + +```cpp +#include +#include + +template +std::string transform(const std::string& str, F&& transformer) { + return str + | std::ranges::views::transform(std::forward(transformer)) + | std::ranges::to(); +} + + + +// Usage: +std::string str = "Hello, World!"; +std::string transformed = transform(str, [](char c){ return std::toupper(c); }); +std::cout << transformed << std::endl; // HELLO, WORLD! +```