-
-
Notifications
You must be signed in to change notification settings - Fork 130
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
Merged
Snippets c++ #103
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e93bcac
[C++] added snippet
majvax 2a6b1e2
Merge remote-tracking branch 'origin/snippets-c++' into snippets-c++
majvax aad3f79
[C++] added 3 snippets for string.
majvax f472a29
Added missing chrono header
majvax 31b43b5
fixed tags
majvax 29112d8
Merge remote-tracking branch 'origin/snippets-c++' into snippets-c++
majvax 4638879
fixed exemple needing std=c++20
majvax 98ddffc
Fixed Usage to accommodate to new guidelines
majvax aebefde
Fixed all tags + type
majvax d76dbfd
Revert "Fixed all tags + type"
majvax c188670
Merge remote-tracking branch 'upstream/main' into snippets-c++
majvax 9364abb
Fixed type and all tags
majvax 3a99176
fixed gh check failing
majvax c0d6f04
Update consolidated snippets
actions-user 1dd6a8f
Merge branch 'dostonnabotov:main' into snippets-c++
majvax fce087f
Update consolidated snippets
actions-user a959e95
Update consolidated snippets
actions-user 4708bd9
Merge remote-tracking branch 'origin/main' into snippets-c++
majvax 112302e
Revert "Merge remote-tracking branch 'origin/main' into snippets-c++"
majvax 60496ec
Merge remote-tracking branch 'origin/main' into snippets-c++
majvax 812842f
Update consolidated snippets
actions-user File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
Title: Filter Vector | ||
Description: Filters a vector using a predicate function. | ||
Author: majvax | ||
Tags: cpp,array,filter,utility | ||
--- | ||
|
||
```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>>(); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
Title: Transform Vector | ||
Description: Transforms a vector using a function. | ||
Author: majvax | ||
Tags: cpp,array,transform,utility | ||
--- | ||
|
||
```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>>(); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
Title: Find files | ||
Description: Find all the files in a directory and subdirectories using a predicate function. | ||
Author: majvax | ||
Tags: cpp,array,filesystem,file_search,recursive_search | ||
--- | ||
|
||
```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> | ||
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; | ||
}), | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
Title: Find files | ||
Description: Find all the files in a directory using a predicate function. | ||
Author: majvax | ||
Tags: cpp,array,filesystem,file_search | ||
--- | ||
|
||
```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> | ||
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; | ||
}), | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
Title: List Directories | ||
Description: Lists all the directories in a path. | ||
Author: majvax | ||
Tags: cpp,array,filesystem,directories | ||
--- | ||
|
||
```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; | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: Filter | ||
description: Filter a string with a predicate function | ||
author: majvax | ||
tags: cpp,string,utility,filtering | ||
--- | ||
|
||
```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>(); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: Palindrome | ||
description: Check if a string is a palindrome or not. | ||
author: majvax | ||
tags: cpp,string,utility | ||
--- | ||
|
||
```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); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: Transform | ||
description: Transform a string with a function | ||
author: majvax | ||
tags: cpp,string,utility,transform | ||
--- | ||
|
||
```cpp | ||
#include <ranges> | ||
#include <string> | ||
|
||
template <typename F> | ||
std::string transform(const std::string& str, F&& transformer) { | ||
return str | ||
| std::ranges::views::transform(std::forward<F>(transformer)) | ||
| std::ranges::to<std::string>(); | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.