Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 145a139

Browse files
Merge remote-tracking branch 'dist/main'
2 parents 8c70093 + 52e7c9c commit 145a139

20 files changed

+437
-32
lines changed

‎CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ If your function doesn't return anything just show how to use it. If the result
6565

6666
To ensure your snippet isn’t refused, consider these questions:
6767
- **Does the standard library of my language provide an easy way of doing this ?**
68-
- **Does that snippet have a real, and practical use case ?**
68+
- **Does that snippet not have a real, and practical use case ?**
6969
- **Could it be split into separate parts to be better understood ?**
7070

7171
If any answer is yes, then your snippet will most likely get rejected.
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+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Palindrome
3+
description: Check if a string is a palindrome or not.
4+
author: majvax
5+
tags: string,c++23
6+
---
7+
8+
```cpp
9+
#include <string>
10+
#include <ranges>
11+
#include <algorithm>
12+
13+
bool is_palindrome(const std::string& str) {
14+
std::string sanitized_string = str
15+
| std::ranges::views::filter([](char c){ return std::isalnum(c); })
16+
| std::ranges::views::transform([](char c){ return std::tolower(c); })
17+
| std::ranges::to<std::string>();
18+
19+
return std::ranges::equal(sanitized_string, sanitized_string | std::views::reverse);
20+
}
21+
22+
23+
24+
// Usage:
25+
bool pal = is_palindrome("A man, a plan, a canal, Panama"); // true
26+
```

‎snippets/cpp/string-manipulation/reverse-string.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Reverse String
33
description: Reverses the characters in a string.
44
author: Vaibhav-kesarwani
5-
tags: array,reverse
5+
tags: array,reverse,c++23
66
---
77

88
```cpp
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Transform
3+
description: Transform a string with a function
4+
author: majvax
5+
tags: string,transform,c++23
6+
---
7+
8+
```cpp
9+
#include <ranges>
10+
#include <string>
11+
12+
template <typename F>
13+
std::string transform(const std::string& str, F&& transformer) {
14+
return str
15+
| std::ranges::views::transform(std::forward<F>(transformer))
16+
| std::ranges::to<std::string>();
17+
}
18+
19+
20+
21+
// Usage:
22+
std::string str = "Hello, World!";
23+
std::string transformed = transform(str, [](char c){ return std::toupper(c); });
24+
std::cout << transformed << std::endl; // HELLO, WORLD!
25+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /