Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Snippets c++ #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
saminjay merged 21 commits into quicksnip-dev:main from majvax:snippets-c++
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e93bcac
[C++] added snippet
majvax Jan 1, 2025
2a6b1e2
Merge remote-tracking branch 'origin/snippets-c++' into snippets-c++
majvax Jan 1, 2025
aad3f79
[C++] added 3 snippets for string.
majvax Jan 1, 2025
f472a29
Added missing chrono header
majvax Jan 2, 2025
31b43b5
fixed tags
majvax Jan 2, 2025
29112d8
Merge remote-tracking branch 'origin/snippets-c++' into snippets-c++
majvax Jan 2, 2025
4638879
fixed exemple needing std=c++20
majvax Jan 2, 2025
98ddffc
Fixed Usage to accommodate to new guidelines
majvax Jan 2, 2025
aebefde
Fixed all tags + type
majvax Jan 3, 2025
d76dbfd
Revert "Fixed all tags + type"
majvax Jan 3, 2025
c188670
Merge remote-tracking branch 'upstream/main' into snippets-c++
majvax Jan 3, 2025
9364abb
Fixed type and all tags
majvax Jan 3, 2025
3a99176
fixed gh check failing
majvax Jan 3, 2025
c0d6f04
Update consolidated snippets
actions-user Jan 3, 2025
1dd6a8f
Merge branch 'dostonnabotov:main' into snippets-c++
majvax Jan 3, 2025
fce087f
Update consolidated snippets
actions-user Jan 3, 2025
a959e95
Update consolidated snippets
actions-user Jan 3, 2025
4708bd9
Merge remote-tracking branch 'origin/main' into snippets-c++
majvax Jan 4, 2025
112302e
Revert "Merge remote-tracking branch 'origin/main' into snippets-c++"
majvax Jan 6, 2025
60496ec
Merge remote-tracking branch 'origin/main' into snippets-c++
majvax Jan 6, 2025
812842f
Update consolidated snippets
actions-user Jan 6, 2025
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
108 changes: 107 additions & 1 deletion public/consolidated/cpp.json
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
[
{
"categoryName": "Array Manipulation",
"snippets": [
{
"title": "Filter Vector",
"description": "Filters a vector using a predicate function.",
"author": "majvax",
"tags": [
"array",
"filter",
"c++23"
],
"contributors": [],
"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"
},
{
"title": "Transform Vector",
"description": "Transforms a vector using a function.",
"author": "majvax",
"tags": [
"array",
"transform",
"c++23"
],
"contributors": [],
"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"
}
]
},
{
"categoryName": "Basics",
"snippets": [
Expand Down Expand Up @@ -32,6 +61,47 @@
}
]
},
{
"categoryName": "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 <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"
},
{
"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 <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"
},
{
"title": "List Directories",
"description": "Lists all the directories in a path.",
"author": "majvax",
"tags": [
"filesystem",
"directories",
"c++17"
],
"contributors": [],
"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"
}
]
},
{
"categoryName": "Math And Numbers",
"snippets": [
Expand All @@ -51,13 +121,37 @@
{
"categoryName": "String Manipulation",
"snippets": [
{
"title": "Filter",
"description": "Filter a string with a predicate function",
"author": "majvax",
"tags": [
"string",
"filtering",
"c++23"
],
"contributors": [],
"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"
},
{
"title": "Palindrome",
"description": "Check if a string is a palindrome or not.",
"author": "majvax",
"tags": [
"string",
"c++23"
],
"contributors": [],
"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"
},
{
"title": "Reverse String",
"description": "Reverses the characters in a string.",
"author": "Vaibhav-kesarwani",
"tags": [
"array",
"reverse"
"reverse",
"c++23"
],
"contributors": [],
"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"
Expand All @@ -72,6 +166,18 @@
],
"contributors": [],
"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"
},
{
"title": "Transform",
"description": "Transform a string with a function",
"author": "majvax",
"tags": [
"string",
"transform",
"c++23"
],
"contributors": [],
"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"
}
]
}
Expand Down
25 changes: 25 additions & 0 deletions snippets/cpp/array-manipulation/filter-vector.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
Title: Filter Vector
Description: Filters a vector using a predicate function.
Author: majvax
Tags: array,filter,c++23
---

```cpp
#include <ranges>
#include <vector>

template <typename T, typename P>
auto filter(const std::vector<T>& vec, P&& predicate) {
return vec
| std::views::filter(std::forward<P>(predicate))
| std::ranges::to<std::vector<T>>();
}



// Usage:
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int> filtered = filter(vec, [](int i){ return i % 2 == 0; });
// filtered contains 2 and 4
```
26 changes: 26 additions & 0 deletions snippets/cpp/array-manipulation/transform-vector.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
Title: Transform Vector
Description: Transforms a vector using a function.
Author: majvax
Tags: array,transform,c++23
---

```cpp
#include <ranges>
#include <vector>

template <typename T, typename F>
auto transform(const std::vector<T>& vec, F&& transformer) {
using U = std::invoke_result_t<F, T>;
return vec
| std::views::transform(std::forward<F>(transformer))
| std::ranges::to<std::vector<U>>();
}



// Usage:
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int> transformed = transform(vec, [](int i){ return i * 2; });
// transformed contains 2, 4, 6, 8, 10
```
57 changes: 57 additions & 0 deletions snippets/cpp/file-handling/find-files-recursively.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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 <filesystem>
#include <vector>
#include <string>

template <typename P>
std::vector<std::filesystem::path> find_files_recursive(const std::string& path, P&& predicate) {
std::vector<std::filesystem::path> 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 <chrono>
// 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;
}),
```
57 changes: 57 additions & 0 deletions snippets/cpp/file-handling/find-files.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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 <filesystem>
#include <vector>
#include <string>

template <typename P>
std::vector<std::filesystem::path> find_files(const std::string& path, P&& predicate) {
std::vector<std::filesystem::path> 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 <chrono>
// 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;
}),
```
37 changes: 37 additions & 0 deletions snippets/cpp/file-handling/list-directories.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
Title: List Directories
Description: Lists all the directories in a path.
Author: majvax
Tags: filesystem,directories,c++17
---

```cpp
#include <filesystem>
#include <vector>
#include <string>

std::vector<std::filesystem::path> list_directories(const std::string& path) {
std::vector<std::filesystem::path> 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");
```
25 changes: 25 additions & 0 deletions snippets/cpp/string-manipulation/filter.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Filter
description: Filter a string with a predicate function
author: majvax
tags: string,filtering,c++23
---

```cpp
#include <ranges>
#include <string>

template <typename P>
std::string filter(const std::string& str, P&& predicate) {
return str
| std::ranges::views::filter(std::forward<P>(predicate))
| std::ranges::to<std::string>();
}



// Usage:
std::string str = "Hello, World!";
std::string filtered = filter(str, [](char c){ return std::isalpha(c); });
std::cout << filtered << std::endl; // HelloWorld
```
26 changes: 26 additions & 0 deletions snippets/cpp/string-manipulation/palindrome.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Palindrome
description: Check if a string is a palindrome or not.
author: majvax
tags: string,c++23
---

```cpp
#include <string>
#include <ranges>
#include <algorithm>

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<std::string>();

return std::ranges::equal(sanitized_string, sanitized_string | std::views::reverse);
}



// Usage:
bool pal = is_palindrome("A man, a plan, a canal, Panama"); // true
```
Loading
Loading

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /