Skip to content

Commit dd9c1a1

Browse files
authored
Merge pull request #677 from ckormanyos/update_docs
Update docs and snippets
2 parents bfcc24d + 113b5f3 commit dd9c1a1

2 files changed

Lines changed: 34 additions & 42 deletions

File tree

code_snippets/chapter03/chapter03_11-001_array.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,33 @@
77

88
// chapter03_11-001_array.cpp
99

10-
#include <algorithm>
1110
#include <array>
12-
#include <cstddef>
1311
#include <cstdint>
12+
#include <cstddef>
13+
#include <format>
1414
#include <iostream>
15-
#include <iomanip>
16-
#include <iterator>
15+
#include <string>
1716

18-
using login_key_array_type = std::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(3))>;
17+
using login_key_array_type = std::array<std::uint8_t, std::size_t { UINT8_C(3) }>;
1918

2019
// A login key stored in an std::array.
2120

22-
constexpr auto login_key =
23-
login_key_array_type
21+
constexpr login_key_array_type
22+
login_key
2423
{
25-
static_cast<std::uint8_t>(UINT8_C(0x01)),
26-
static_cast<std::uint8_t>(UINT8_C(0x02)),
27-
static_cast<std::uint8_t>(UINT8_C(0x03))
24+
UINT8_C(0x01),
25+
UINT8_C(0x02),
26+
UINT8_C(0x03)
2827
};
2928

3029
auto main() -> int
3130
{
32-
const auto flg = std::cout.flags();
31+
std::string str_login_key { "login_key is: " };
3332

34-
std::cout.unsetf(std::ios::dec);
35-
std::cout.setf(std::ios::hex);
36-
std::cout.setf(std::ios::showbase);
37-
38-
std::cout << "login_key is ";
39-
std::copy(login_key.cbegin(), login_key.cend(), std::ostream_iterator<unsigned int>(std::cout, ","));
40-
std::cout << std::endl;
33+
for(const auto& value : login_key)
34+
{
35+
str_login_key += std::format("{:#x},", value);
36+
}
4137

42-
std::cout.flags(flg);
38+
std::cout << str_login_key << std::endl;
4339
}

code_snippets/readme.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Real-Time-C++ - Code Snippets
66
<img src="https://github.com/ckormanyos/real-time-cpp/actions/workflows/real-time-cpp-snippets.yml/badge.svg" alt="Build Snippets"></a>
77
<a href="https://github.com/ckormanyos/real-time-cpp/blob/master/LICENSE_1_0.txt">
88
<img src="https://img.shields.io/badge/license-BSL%201.0-blue.svg" alt="Boost Software License 1.0"></a>
9-
<a href="https://godbolt.org/z/qfafePavo" alt="godbolt">
9+
<a href="https://godbolt.org/z/9MYejn7cT" alt="godbolt">
1010
<img src="https://img.shields.io/badge/try%20it%20on-godbolt-green" /></a>
1111
</p>
1212

@@ -37,39 +37,35 @@ Consider, for instance, the code snippet
3737
```cpp
3838
// chapter03_11-001_array.cpp
3939

40-
#include <algorithm>
4140
#include <array>
42-
#include <cstddef>
4341
#include <cstdint>
42+
#include <cstddef>
43+
#include <format>
4444
#include <iostream>
45-
#include <iomanip>
46-
#include <iterator>
45+
#include <string>
4746

48-
using login_key_array_type = std::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(3))>;
47+
using login_key_array_type = std::array<std::uint8_t, std::size_t { UINT8_C(3) }>;
4948

5049
// A login key stored in an std::array.
5150

52-
constexpr auto login_key =
53-
login_key_array_type
51+
constexpr login_key_array_type
52+
login_key
5453
{
55-
static_cast<std::uint8_t>(UINT8_C(0x01)),
56-
static_cast<std::uint8_t>(UINT8_C(0x02)),
57-
static_cast<std::uint8_t>(UINT8_C(0x03))
54+
UINT8_C(0x01),
55+
UINT8_C(0x02),
56+
UINT8_C(0x03)
5857
};
5958

6059
auto main() -> int
6160
{
62-
const auto flg = std::cout.flags();
61+
std::string str_login_key { "login_key is: " };
6362

64-
std::cout.unsetf(std::ios::dec);
65-
std::cout.setf(std::ios::hex);
66-
std::cout.setf(std::ios::showbase);
67-
68-
std::cout << "login_key is ";
69-
std::copy(login_key.cbegin(), login_key.cend(), std::ostream_iterator<unsigned int>(std::cout, ","));
70-
std::cout << std::endl;
63+
for(const auto& value : login_key)
64+
{
65+
str_login_key += std::format("{:#x},", value);
66+
}
7167

72-
std::cout.flags(flg);
68+
std::cout << str_login_key << std::endl;
7369
}
7470
```
7571
@@ -81,7 +77,7 @@ output console.
8177
The expected output message text is
8278
8379
```sh
84-
login_key is 0x1,0x2,0x3,
80+
login_key is: 0x1,0x2,0x3,
8581
```
8682

8783
## Testing and CI
@@ -93,11 +89,11 @@ using (at the moment) build-only.
9389
## Try it at _godbolt_
9490

9591
<p align="center">
96-
<a href="https://godbolt.org/z/qfafePavo" alt="godbolt">
92+
<a href="https://godbolt.org/z/9MYejn7cT" alt="godbolt">
9793
<img src="https://img.shields.io/badge/try%20it%20on-godbolt-green" /></a>
9894
</p>
9995

100-
Use this [short link](https://godbolt.org/z/qfafePavo)
96+
Use this [short link]https://godbolt.org/z/9MYejn7cT)
10197
to [godbolt](https://godbolt.org) in order to further explore this program.
10298

10399
In the afore-mentioned link, the file

0 commit comments

Comments
 (0)