Skip to content

Commit 3dee709

Browse files
majvaxactions-user
andauthored
Snippets c++ (#103)
* [C++] added snippet * [C++] added 3 snippets for string. * Added missing chrono header * fixed tags * fixed exemple needing std=c++20 * Fixed Usage to accommodate to new guidelines * Fixed all tags + type * Revert "Fixed all tags + type" This reverts commit aebefde. * Fixed type and all tags * fixed gh check failing * Update consolidated snippets * Update consolidated snippets * Update consolidated snippets * Revert "Merge remote-tracking branch 'origin/main' into snippets-c++" This reverts commit 4708bd9, reversing changes made to a959e95. * Update consolidated snippets --------- Co-authored-by: GitHub Action <action@github.com>
1 parent 34bd82f commit 3dee709

File tree

11 files changed

+387
-3
lines changed

11 files changed

+387
-3
lines changed

public/consolidated/cpp.json

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
[
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+
},
231
{
332
"name": "Basics",
433
"snippets": [
@@ -66,6 +95,47 @@
6695
}
6796
]
6897
},
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+
},
69139
{
70140
"name": "Math And Numbers",
71141
"snippets": [
@@ -85,13 +155,37 @@
85155
{
86156
"name": "String Manipulation",
87157
"snippets": [
158+
{
159+
"title": "Filter",
160+
"description": "Filter a string with a predicate function",
161+
"author": "majvax",
162+
"tags": [
163+
"string",
164+
"filtering",
165+
"c++23"
166+
],
167+
"contributors": [],
168+
"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"
169+
},
170+
{
171+
"title": "Palindrome",
172+
"description": "Check if a string is a palindrome or not.",
173+
"author": "majvax",
174+
"tags": [
175+
"string",
176+
"c++23"
177+
],
178+
"contributors": [],
179+
"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"
180+
},
88181
{
89182
"title": "Reverse String",
90183
"description": "Reverses the characters in a string.",
91184
"author": "Vaibhav-kesarwani",
92185
"tags": [
93186
"array",
94-
"reverse"
187+
"reverse",
188+
"c++23"
95189
],
96190
"contributors": [],
97191
"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"
@@ -106,6 +200,18 @@
106200
],
107201
"contributors": [],
108202
"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"
203+
},
204+
{
205+
"title": "Transform",
206+
"description": "Transform a string with a function",
207+
"author": "majvax",
208+
"tags": [
209+
"string",
210+
"transform",
211+
"c++23"
212+
],
213+
"contributors": [],
214+
"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"
109215
}
110216
]
111217
}

public/consolidated/javascript.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@
410410
"algebra"
411411
],
412412
"contributors": [],
413-
"code": "function combinations(n, r) {\n function factorial(x) {\n if (x === 0 || x === 1) return 1;\n let result = 1;\n for (let i = 2; i <= x; i++) {\n result *= i;\n }\n return result;\n }\n return factorial(n) / (factorial(r) * factorial(n - r));\n}\n\n// Usage:\ncombinations(12,24); // Returns: 7.720248753351544e-16\ncombinations(1,22); // Returns: 8.896791392450574e-22\n"
413+
"code": "function combinations(n, r) {\n if (n < 0 || r < 0 || n < r) {\n throw new Error('Invalid input: n and r must be non-negative and n must be greater than or equal to r.');\n }\n\n function factorial(x) {\n if (x === 0 || x === 1) return 1;\n let result = 1;\n for (let i = 2; i <= x; i++) {\n result *= i;\n }\n return result;\n }\n\n const numerator = factorial(n);\n const denominator = factorial(r) * factorial(n - r);\n return numerator / denominator;\n}\n\n// Usage:\ncombinations(24,22); // Returns: 276\ncombinations(5,3); // Returns: 10\n"
414414
},
415415
{
416416
"title": "Cross Product",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
Title: Filter Vector
3+
Description: Filters a vector using a predicate function.
4+
Author: majvax
5+
Tags: array,filter,c++23
6+
---
7+
8+
```cpp
9+
#include <ranges>
10+
#include <vector>
11+
12+
template <typename T, typename P>
13+
auto filter(const std::vector<T>& vec, P&& predicate) {
14+
return vec
15+
| std::views::filter(std::forward<P>(predicate))
16+
| std::ranges::to<std::vector<T>>();
17+
}
18+
19+
20+
21+
// Usage:
22+
std::vector<int> vec = {1, 2, 3, 4, 5};
23+
std::vector<int> filtered = filter(vec, [](int i){ return i % 2 == 0; });
24+
// filtered contains 2 and 4
25+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
Title: Transform Vector
3+
Description: Transforms a vector using a function.
4+
Author: majvax
5+
Tags: array,transform,c++23
6+
---
7+
8+
```cpp
9+
#include <ranges>
10+
#include <vector>
11+
12+
template <typename T, typename F>
13+
auto transform(const std::vector<T>& vec, F&& transformer) {
14+
using U = std::invoke_result_t<F, T>;
15+
return vec
16+
| std::views::transform(std::forward<F>(transformer))
17+
| std::ranges::to<std::vector<U>>();
18+
}
19+
20+
21+
22+
// Usage:
23+
std::vector<int> vec = {1, 2, 3, 4, 5};
24+
std::vector<int> transformed = transform(vec, [](int i){ return i * 2; });
25+
// transformed contains 2, 4, 6, 8, 10
26+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
Title: Find files recursively
3+
Description: Find all the files in a directory and subdirectories using a predicate function.
4+
Author: majvax
5+
Tags: filesystem,file_search,c++17
6+
---
7+
8+
```cpp
9+
#include <filesystem>
10+
#include <vector>
11+
#include <string>
12+
13+
template <typename P>
14+
std::vector<std::filesystem::path> find_files_recursive(const std::string& path, P&& predicate) {
15+
std::vector<std::filesystem::path> files;
16+
std::error_code ec;
17+
18+
if (!std::filesystem::exists(path, ec) || ec)
19+
return files;
20+
if (!std::filesystem::is_directory(path, ec) || ec)
21+
return files;
22+
23+
auto it = std::filesystem::recursive_directory_iterator(path, ec);
24+
if (ec)
25+
return files;
26+
27+
for (const auto& entry : it)
28+
if (!std::filesystem::is_directory(entry) && predicate(entry.path()))
29+
files.push_back(entry.path());
30+
31+
return files;
32+
}
33+
34+
35+
36+
// Usage:
37+
38+
// Find all files with size greater than 10MB
39+
auto files = find_files_recursive("Path", [](const auto& p) {
40+
return std::filesystem::file_size(p) > 10 * 1024 * 1024;
41+
});
42+
43+
// Find all files with ".pdf" as extension
44+
auto files = find_files_recursive("Path", [](const auto& p) {
45+
return p.extension() == ".pdf";
46+
});
47+
48+
// Find all files writed after The New Year
49+
#include <chrono>
50+
// need std=c++20
51+
auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys(
52+
std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}}
53+
);
54+
auto files = find_files_recursive("Path", [jan_1_2025](const auto& p) {
55+
return std::filesystem::last_write_time(p) > jan_1_2025;
56+
}),
57+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
Title: Find files
3+
Description: Find all the files in a directory using a predicate function.
4+
Author: majvax
5+
Tags: filesystem,file_search,c++17
6+
---
7+
8+
```cpp
9+
#include <filesystem>
10+
#include <vector>
11+
#include <string>
12+
13+
template <typename P>
14+
std::vector<std::filesystem::path> find_files(const std::string& path, P&& predicate) {
15+
std::vector<std::filesystem::path> files;
16+
std::error_code ec;
17+
18+
if (!std::filesystem::exists(path, ec) || ec)
19+
return files;
20+
if (!std::filesystem::is_directory(path, ec) || ec)
21+
return files;
22+
23+
auto it = std::filesystem::directory_iterator(path, ec);
24+
if (ec)
25+
return files;
26+
27+
for (const auto& entry : it)
28+
if (!std::filesystem::is_directory(entry) && predicate(entry.path()))
29+
files.push_back(entry.path());
30+
31+
return files;
32+
}
33+
34+
35+
36+
// Usage:
37+
38+
// Find all files with size greater than 10MB
39+
auto files = find_files("Path", [](const auto& p) {
40+
return std::filesystem::file_size(p) > 10 * 1024 * 1024;
41+
});
42+
43+
// Find all files with ".pdf" as extension
44+
auto files = find_files("Path", [](const auto& p) {
45+
return p.extension() == ".pdf";
46+
});
47+
48+
// Find all files writed after The New Year
49+
#include <chrono>
50+
// need std=c++20
51+
auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys(
52+
std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}}
53+
);
54+
auto files = find_files("Path", [jan_1_2025](const auto& p) {
55+
return std::filesystem::last_write_time(p) > jan_1_2025;
56+
}),
57+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
Title: List Directories
3+
Description: Lists all the directories in a path.
4+
Author: majvax
5+
Tags: filesystem,directories,c++17
6+
---
7+
8+
```cpp
9+
#include <filesystem>
10+
#include <vector>
11+
#include <string>
12+
13+
std::vector<std::filesystem::path> list_directories(const std::string& path) {
14+
std::vector<std::filesystem::path> files;
15+
std::error_code ec;
16+
17+
if (!std::filesystem::exists(path, ec) || ec)
18+
return files;
19+
if (!std::filesystem::is_directory(path, ec) || ec)
20+
return files;
21+
22+
auto it = std::filesystem::directory_iterator(path, ec);
23+
if (ec)
24+
return files;
25+
26+
for (const auto& entry : it)
27+
if (std::filesystem::is_directory(entry))
28+
files.push_back(entry.path());
29+
30+
return files;
31+
}
32+
33+
34+
35+
// Usage:
36+
auto directories = list_directories("Path");
37+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Filter
3+
description: Filter a string with a predicate function
4+
author: majvax
5+
tags: string,filtering,c++23
6+
---
7+
8+
```cpp
9+
#include <ranges>
10+
#include <string>
11+
12+
template <typename P>
13+
std::string filter(const std::string& str, P&& predicate) {
14+
return str
15+
| std::ranges::views::filter(std::forward<P>(predicate))
16+
| std::ranges::to<std::string>();
17+
}
18+
19+
20+
21+
// Usage:
22+
std::string str = "Hello, World!";
23+
std::string filtered = filter(str, [](char c){ return std::isalpha(c); });
24+
std::cout << filtered << std::endl; // HelloWorld
25+
```

0 commit comments

Comments
 (0)