Skip to content
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

[c++] added few c++ snippets #53

Closed
wants to merge 12 commits into from
40 changes: 40 additions & 0 deletions public/data/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,45 @@
"author": "saminjay"
}
]
},
{
"categoryName": "Array Manipulation",
"snippets": [
{
"title": "Transform Vector",
"description": "Transforms a vector using a function",
"code": [
"#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>>();",
"}"
],
"tags": ["cpp", "array", "transform", "utility"],
"author": "majvax"
},
{
"title": "Filter Vector",
"description": "Filters a vector using a predicate function",
"code": [
"#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>>();",
"}"
],
"tags": ["cpp", "array", "filter", "utility"],
"author": "majvax"
}
]
}
]