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 aad3f79

Browse files
committed
[C++] added 3 snippets for string.
1 parent 2a6b1e2 commit aad3f79

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Filter
3+
description: Filter a string with a predicate function
4+
author: majvax
5+
tags: cpp,string,utility,filtering
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+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Palindrome
3+
description: Check if a string is a palindrome or not.
4+
author: majvax
5+
tags: cpp,string,utility
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+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Transform
3+
description: Transform a string with a function
4+
author: majvax
5+
tags: cpp,string,utility,transform
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+
```

0 commit comments

Comments
(0)

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