Skip to content

Commit 8d101bd

Browse files
committed
[C++] added few snippets
1 parent 711b560 commit 8d101bd

File tree

6 files changed

+178
-3
lines changed

6 files changed

+178
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
Title: Filter Vector
3+
Description: Filters a vector using a predicate function.
4+
Author: majvax
5+
Tags: cpp,array,filter,utility
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+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
Title: Transform Vector
3+
Description: Transforms a vector using a function.
4+
Author: majvax
5+
Tags: cpp,array,transform,utility
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+
```

snippets/cpp/basics/hello-world.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: Hello, World!
3-
description: Prints Hello, World! to the terminal.
2+
Tags: cpp,hello-world
43
author: James-Beans
5-
tags: cpp,printing,hello-world,utility
4+
description: Prints Hello, World! to the terminal.
5+
title: Hello, World!
66
---
77

88
```cpp
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
Title: Find files
3+
Description: Find all the files in a directory and subdirectories using a predicate function.
4+
Author: majvax
5+
Tags: cpp,array,filesystem,file_search,recursive_search
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+
// usage:
36+
// Find all files with size greater than 10MB
37+
auto files = find_files_recursive("Path", [](const auto& p) {
38+
return std::filesystem::file_size(p) > 10 * 1024 * 1024;
39+
});
40+
41+
// Find all files with ".pdf" as extension
42+
auto files = find_files_recursive("Path", [](const auto& p) {
43+
return p.extension() == ".pdf";
44+
});
45+
46+
// Find all files writed after The New Year
47+
auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys(
48+
std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}}
49+
);
50+
auto files = find_files_recursive("Path", [jan_1_2025](const auto& p) {
51+
return std::filesystem::last_write_time(p) > jan_1_2025;
52+
}),
53+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
Title: Find files
3+
Description: Find all the files in a directory using a predicate function.
4+
Author: majvax
5+
Tags: cpp,array,filesystem,file_search
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+
// usage:
36+
// Find all files with size greater than 10MB
37+
auto files = find_files("Path", [](const auto& p) {
38+
return std::filesystem::file_size(p) > 10 * 1024 * 1024;
39+
});
40+
41+
// Find all files with ".pdf" as extension
42+
auto files = find_files("Path", [](const auto& p) {
43+
return p.extension() == ".pdf";
44+
});
45+
46+
// Find all files writed after The New Year
47+
auto jan_1_2025 = std::filesystem::file_time_type::clock::from_sys(
48+
std::chrono::sys_days{std::chrono::year{2025}/std::chrono::month{1}/std::chrono::day{1}}
49+
);
50+
auto files = find_files("Path", [jan_1_2025](const auto& p) {
51+
return std::filesystem::last_write_time(p) > jan_1_2025;
52+
}),
53+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
Title: List Directories
3+
Description: Lists all the directories in a path.
4+
Author: majvax
5+
Tags: cpp,array,filesystem,directories
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+
```

0 commit comments

Comments
 (0)