mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-18 02:24:35 +03:00
Add std::sample to C++17 section.
This commit is contained in:
14
README.md
14
README.md
@@ -63,7 +63,7 @@ C++17 includes the following new library features:
|
||||
- [std::byte](#stdbyte)
|
||||
- [splicing for maps and sets](#splicing-for-maps-and-sets)
|
||||
- [parallel algorithms](#parallel-algorithms)
|
||||
|
||||
- [std::sample](#stdsample)
|
||||
|
||||
C++14 includes the following new language features:
|
||||
- [binary literals](#binary-literals)
|
||||
@@ -1184,6 +1184,18 @@ auto result1 = std::find(std::execution::par, std::begin(longVector), std::end(l
|
||||
auto result2 = std::sort(std::execution::seq, std::begin(longVector), std::end(longVector));
|
||||
```
|
||||
|
||||
### std::sample
|
||||
Samples n elements in the given sequence (without replacement) where every element has an equal chance of being selected.
|
||||
```c++
|
||||
const std::string ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
std::string guid;
|
||||
// Sample 5 characters from ALLOWED_CHARS.
|
||||
std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid),
|
||||
5, std::mt19937{ std::random_device{}() });
|
||||
|
||||
std::cout << guid; // e.g. G1fW2
|
||||
```
|
||||
|
||||
## C++14 Language Features
|
||||
|
||||
### Binary literals
|
||||
|
||||
Reference in New Issue
Block a user