Skip to content

Commit b6c4515

Browse files
Add std::sample to C++17 section.
1 parent e2f9058 commit b6c4515

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

CPP17.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ C++17 includes the following new library features:
3131
- [std::byte](#stdbyte)
3232
- [splicing for maps and sets](#splicing-for-maps-and-sets)
3333
- [parallel algorithms](#parallel-algorithms)
34+
- [std::sample](#stdsample)
3435

3536
## C++17 Language Features
3637

@@ -522,6 +523,18 @@ auto result1 = std::find(std::execution::par, std::begin(longVector), std::end(l
522523
auto result2 = std::sort(std::execution::seq, std::begin(longVector), std::end(longVector));
523524
```
524525

526+
### std::sample
527+
Samples n elements in the given sequence (without replacement) where every element has an equal chance of being selected.
528+
```c++
529+
const std::string ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
530+
std::string guid;
531+
// Sample 5 characters from ALLOWED_CHARS.
532+
std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid),
533+
5, std::mt19937{ std::random_device{}() });
534+
535+
std::cout << guid; // e.g. G1fW2
536+
```
537+
525538
## Acknowledgements
526539
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
527540
* [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ C++17 includes the following new library features:
6363
- [std::byte](#stdbyte)
6464
- [splicing for maps and sets](#splicing-for-maps-and-sets)
6565
- [parallel algorithms](#parallel-algorithms)
66-
66+
- [std::sample](#stdsample)
6767

6868
C++14 includes the following new language features:
6969
- [binary literals](#binary-literals)
@@ -1184,6 +1184,18 @@ auto result1 = std::find(std::execution::par, std::begin(longVector), std::end(l
11841184
auto result2 = std::sort(std::execution::seq, std::begin(longVector), std::end(longVector));
11851185
```
11861186

1187+
### std::sample
1188+
Samples n elements in the given sequence (without replacement) where every element has an equal chance of being selected.
1189+
```c++
1190+
const std::string ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1191+
std::string guid;
1192+
// Sample 5 characters from ALLOWED_CHARS.
1193+
std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid),
1194+
5, std::mt19937{ std::random_device{}() });
1195+
1196+
std::cout << guid; // e.g. G1fW2
1197+
```
1198+
11871199
## C++14 Language Features
11881200
11891201
### Binary literals

0 commit comments

Comments
 (0)