You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add 'since C++NN' markers to the container features. The existing
motivation (std::array vs vector, the tuple intro, string_view/byte/pmr
sections) already reads well, so no prose rewrite was needed here.
`std::forward_list` is a list container, and the usage is similar to `std::list`, so we don't spend a lot of time introducing it.
107
111
108
112
Need to know is that, unlike the implementation of the doubly linked list of `std::list`, `std::forward_list` is implemented using a singly linked list.
@@ -111,6 +115,8 @@ It is also the only container in the standard library container that does not pr
111
115
112
116
## 4.2 Unordered Container
113
117
118
+
*(since C++11)*
119
+
114
120
We are already familiar with the ordered container `std::map`/`std::set` in traditional C++. These elements are internally implemented by red-black trees.
115
121
The average complexity of inserts and searches is `O(log(size))`. When inserting an element, the element size is compared according to the `<` operator and the element is determined to be the same.
116
122
And select the appropriate location to insert into the container. When traversing the elements in this container, the output will be traversed one by one in the order of the `<` operator.
@@ -177,6 +183,8 @@ But the flaw of `std::pair` is obvious, only two elements can be saved.
177
183
178
184
### Basic Operations
179
185
186
+
*(since C++11)*
187
+
180
188
There are three core functions for the use of tuples:
`std::string_view`, introduced in C++17, is a **non-owning, read-only** view over a sequence of characters; it holds just a pointer and a length. Declaring a parameter as `std::string_view` accepts both a `std::string` and a string literal, **without any copy or allocation**:
334
344
335
345
```cpp
@@ -350,6 +360,8 @@ Mind its **lifetime**: a `string_view` does not own the underlying data, so the
350
360
351
361
### `std::byte`
352
362
363
+
*(since C++17)*
364
+
353
365
`std::byte` represents a single byte of **raw memory**. Unlike `char` or `unsigned char`, it is not an arithmetic type — the standard defines only bitwise operators for it, which prevents accidental arithmetic on raw bytes at the type level:
354
366
355
367
```cpp
@@ -363,6 +375,8 @@ int v = std::to_integer<int>(b); // explicit conversion to an integer: 49
363
375
364
376
## 4.5 Associative container improvements
365
377
378
+
*(since C++17)*
379
+
366
380
C++17 added several more precise and more efficient operations to associative containers such as `std::map` / `std::unordered_map`:
367
381
368
382
- `try_emplace`: inserts only when the key is absent; when the key already exists it does **not** modify the existing value nor move from its arguments, which makes it better than `emplace` for "insert if not present".
@@ -387,6 +401,8 @@ m.merge(more); // splice more's nodes into m
387
401
388
402
## 4.6 Polymorphic allocators `std::pmr`
389
403
404
+
*(since C++17)*
405
+
390
406
C++17 introduced the `std::pmr` namespace in `<memory_resource>`, providing polymorphic allocators based on a **memory resource**. It decouples the *where to allocate* policy from the container type: the same `pmr` container backed by different memory resources is still a single type, avoiding the type bloat caused by template allocators.
391
407
392
408
For instance, `std::pmr::monotonic_buffer_resource` can allocate from a pre-prepared buffer (even one on the stack) and frees everything only when the resource is destroyed — ideal for allocation-heavy workloads with a shared lifetime:
0 commit comments