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

Snippets c++ #103

Merged
merged 21 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions snippets/cpp/array-manipulation/filter-vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ auto filter(const std::vector<T>& vec, P&& predicate) {
| 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
```
7 changes: 7 additions & 0 deletions snippets/cpp/array-manipulation/transform-vector.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ auto transform(const std::vector<T>& vec, F&& transformer) {
| 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 containes 2, 4, 6, 8, 10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

```
4 changes: 3 additions & 1 deletion snippets/cpp/file-handling/find-files-recursive.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ std::vector<std::filesystem::path> find_files_recursive(const std::string& path,
}


// usage:

// 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;
Expand Down
4 changes: 3 additions & 1 deletion snippets/cpp/file-handling/find-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ std::vector<std::filesystem::path> find_files(const std::string& path, P&& predi
}


// usage:

// 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;
Expand Down
5 changes: 5 additions & 0 deletions snippets/cpp/file-handling/list-directories.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ std::vector<std::filesystem::path> list_directories(const std::string& path) {

return files;
}



// Usage:
auto directories = list_directories("Path");
```
7 changes: 7 additions & 0 deletions snippets/cpp/string-manipulation/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ std::string filter(const std::string& str, P&& predicate) {
| 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
```
5 changes: 5 additions & 0 deletions snippets/cpp/string-manipulation/palindrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ bool is_palindrome(const std::string& str) {

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



// Usage:
bool pal = is_palindrome("A man, a plan, a canal, Panama"); // true
```
7 changes: 7 additions & 0 deletions snippets/cpp/string-manipulation/transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ std::string transform(const std::string& str, F&& transformer) {
| std::ranges::views::transform(std::forward<F>(transformer))
| std::ranges::to<std::string>();
}



// Usage:
std::string str = "Hello, World!";
std::string transformed = transform(str, [](char c){ return std::toupper(c); });
std::cout << transformed << std::endl; // HELLO, WORLD!
```
Loading