tmp/tmp8xu3u37x/{from.md → to.md}
RENAMED
|
@@ -1,29 +1,28 @@
|
|
| 1 |
### Class template `forward_list` <a id="forwardlist">[[forwardlist]]</a>
|
| 2 |
|
| 3 |
-
####
|
| 4 |
|
| 5 |
A `forward_list` is a container that supports forward iterators and
|
| 6 |
allows constant time insert and erase operations anywhere within the
|
| 7 |
sequence, with storage management handled automatically. Fast random
|
| 8 |
access to list elements is not supported.
|
| 9 |
|
| 10 |
[*Note 1*: It is intended that `forward_list` have zero space or time
|
| 11 |
overhead relative to a hand-written C-style singly linked list. Features
|
| 12 |
that would conflict with that goal have been omitted. — *end note*]
|
| 13 |
|
| 14 |
-
A `forward_list`
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
table or for operations where there is additional semantic information.
|
| 25 |
|
| 26 |
[*Note 2*: Modifying any list requires access to the element preceding
|
| 27 |
the first element of interest, but in a `forward_list` there is no
|
| 28 |
constant-time way to access a preceding element. For this reason, ranges
|
| 29 |
that are modified, such as those supplied to `erase` and `splice`, must
|
|
@@ -32,11 +31,11 @@ be open at the beginning. — *end note*]
|
|
| 32 |
``` cpp
|
| 33 |
namespace std {
|
| 34 |
template<class T, class Allocator = allocator<T>>
|
| 35 |
class forward_list {
|
| 36 |
public:
|
| 37 |
-
// types
|
| 38 |
using value_type = T;
|
| 39 |
using allocator_type = Allocator;
|
| 40 |
using pointer = typename allocator_traits<Allocator>::pointer;
|
| 41 |
using const_pointer = typename allocator_traits<Allocator>::const_pointer;
|
| 42 |
using reference = value_type&;
|
|
@@ -48,15 +47,13 @@ namespace std {
|
|
| 48 |
|
| 49 |
// [forwardlist.cons], construct/copy/destroy
|
| 50 |
forward_list() : forward_list(Allocator()) { }
|
| 51 |
explicit forward_list(const Allocator&);
|
| 52 |
explicit forward_list(size_type n, const Allocator& = Allocator());
|
| 53 |
-
forward_list(size_type n, const T& value,
|
| 54 |
-
const Allocator& = Allocator());
|
| 55 |
template<class InputIterator>
|
| 56 |
-
forward_list(InputIterator first, InputIterator last,
|
| 57 |
-
const Allocator& = Allocator());
|
| 58 |
forward_list(const forward_list& x);
|
| 59 |
forward_list(forward_list&& x);
|
| 60 |
forward_list(const forward_list& x, const Allocator&);
|
| 61 |
forward_list(forward_list&& x, const Allocator&);
|
| 62 |
forward_list(initializer_list<T>, const Allocator& = Allocator());
|
|
@@ -81,12 +78,12 @@ namespace std {
|
|
| 81 |
|
| 82 |
const_iterator cbegin() const noexcept;
|
| 83 |
const_iterator cbefore_begin() const noexcept;
|
| 84 |
const_iterator cend() const noexcept;
|
| 85 |
|
| 86 |
-
// capacity
|
| 87 |
-
bool
|
| 88 |
size_type max_size() const noexcept;
|
| 89 |
|
| 90 |
// [forwardlist.access], element access
|
| 91 |
reference front();
|
| 92 |
const_reference front() const;
|
|
@@ -116,24 +113,22 @@ namespace std {
|
|
| 116 |
void clear() noexcept;
|
| 117 |
|
| 118 |
// [forwardlist.ops], forward_list operations
|
| 119 |
void splice_after(const_iterator position, forward_list& x);
|
| 120 |
void splice_after(const_iterator position, forward_list&& x);
|
| 121 |
-
void splice_after(const_iterator position, forward_list& x,
|
| 122 |
-
|
| 123 |
-
void splice_after(const_iterator position, forward_list&& x,
|
| 124 |
-
const_iterator i);
|
| 125 |
void splice_after(const_iterator position, forward_list& x,
|
| 126 |
const_iterator first, const_iterator last);
|
| 127 |
void splice_after(const_iterator position, forward_list&& x,
|
| 128 |
const_iterator first, const_iterator last);
|
| 129 |
|
| 130 |
-
|
| 131 |
-
template
|
| 132 |
|
| 133 |
-
|
| 134 |
-
template
|
| 135 |
|
| 136 |
void merge(forward_list& x);
|
| 137 |
void merge(forward_list&& x);
|
| 138 |
template<class Compare> void merge(forward_list& x, Compare comp);
|
| 139 |
template<class Compare> void merge(forward_list&& x, Compare comp);
|
|
@@ -142,42 +137,28 @@ namespace std {
|
|
| 142 |
template<class Compare> void sort(Compare comp);
|
| 143 |
|
| 144 |
void reverse() noexcept;
|
| 145 |
};
|
| 146 |
|
| 147 |
-
template<class InputIterator,
|
| 148 |
-
class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
|
| 149 |
forward_list(InputIterator, InputIterator, Allocator = Allocator())
|
| 150 |
-
-> forward_list<
|
| 151 |
|
| 152 |
-
|
| 153 |
-
bool operator==(const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y);
|
| 154 |
-
template <class T, class Allocator>
|
| 155 |
-
bool operator< (const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y);
|
| 156 |
-
template <class T, class Allocator>
|
| 157 |
-
bool operator!=(const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y);
|
| 158 |
-
template <class T, class Allocator>
|
| 159 |
-
bool operator> (const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y);
|
| 160 |
-
template <class T, class Allocator>
|
| 161 |
-
bool operator>=(const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y);
|
| 162 |
-
template <class T, class Allocator>
|
| 163 |
-
bool operator<=(const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y);
|
| 164 |
-
|
| 165 |
-
// [forwardlist.spec], specialized algorithms
|
| 166 |
template<class T, class Allocator>
|
| 167 |
void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
|
| 168 |
noexcept(noexcept(x.swap(y)));
|
| 169 |
}
|
| 170 |
```
|
| 171 |
|
| 172 |
An incomplete type `T` may be used when instantiating `forward_list` if
|
| 173 |
-
the allocator
|
| 174 |
-
[[allocator.requirements.completeness]]
|
| 175 |
any member of the resulting specialization of `forward_list` is
|
| 176 |
referenced.
|
| 177 |
|
| 178 |
-
####
|
| 179 |
|
| 180 |
``` cpp
|
| 181 |
explicit forward_list(const Allocator&);
|
| 182 |
```
|
| 183 |
|
|
@@ -188,26 +169,26 @@ allocator.
|
|
| 188 |
|
| 189 |
``` cpp
|
| 190 |
explicit forward_list(size_type n, const Allocator& = Allocator());
|
| 191 |
```
|
| 192 |
|
|
|
|
|
|
|
| 193 |
*Effects:* Constructs a `forward_list` object with `n` default-inserted
|
| 194 |
elements using the specified allocator.
|
| 195 |
|
| 196 |
-
*Requires:* `T` shall be `DefaultInsertable` into `*this`.
|
| 197 |
-
|
| 198 |
*Complexity:* Linear in `n`.
|
| 199 |
|
| 200 |
``` cpp
|
| 201 |
forward_list(size_type n, const T& value, const Allocator& = Allocator());
|
| 202 |
```
|
| 203 |
|
|
|
|
|
|
|
| 204 |
*Effects:* Constructs a `forward_list` object with `n` copies of `value`
|
| 205 |
using the specified allocator.
|
| 206 |
|
| 207 |
-
*Requires:* `T` shall be `CopyInsertable` into `*this`.
|
| 208 |
-
|
| 209 |
*Complexity:* Linear in `n`.
|
| 210 |
|
| 211 |
``` cpp
|
| 212 |
template<class InputIterator>
|
| 213 |
forward_list(InputIterator first, InputIterator last, const Allocator& = Allocator());
|
|
@@ -216,11 +197,11 @@ template <class InputIterator>
|
|
| 216 |
*Effects:* Constructs a `forward_list` object equal to the range
|
| 217 |
\[`first`, `last`).
|
| 218 |
|
| 219 |
*Complexity:* Linear in `distance(first, last)`.
|
| 220 |
|
| 221 |
-
####
|
| 222 |
|
| 223 |
``` cpp
|
| 224 |
iterator before_begin() noexcept;
|
| 225 |
const_iterator before_begin() const noexcept;
|
| 226 |
const_iterator cbefore_begin() const noexcept;
|
|
@@ -232,20 +213,20 @@ equal to the iterator returned by `begin()`.
|
|
| 232 |
*Effects:* `cbefore_begin()` is equivalent to
|
| 233 |
`const_cast<forward_list const&>(*this).before_begin()`.
|
| 234 |
|
| 235 |
*Remarks:* `before_begin() == end()` shall equal `false`.
|
| 236 |
|
| 237 |
-
####
|
| 238 |
|
| 239 |
``` cpp
|
| 240 |
reference front();
|
| 241 |
const_reference front() const;
|
| 242 |
```
|
| 243 |
|
| 244 |
*Returns:* `*begin()`
|
| 245 |
|
| 246 |
-
####
|
| 247 |
|
| 248 |
None of the overloads of `insert_after` shall affect the validity of
|
| 249 |
iterators and references, and `erase_after` shall invalidate only
|
| 250 |
iterators and references to the erased elements. If an exception is
|
| 251 |
thrown during `insert_after` there shall be no effect. Inserting `n`
|
|
@@ -277,22 +258,22 @@ void pop_front();
|
|
| 277 |
``` cpp
|
| 278 |
iterator insert_after(const_iterator position, const T& x);
|
| 279 |
iterator insert_after(const_iterator position, T&& x);
|
| 280 |
```
|
| 281 |
|
| 282 |
-
*
|
| 283 |
iterator in the range \[`begin()`, `end()`).
|
| 284 |
|
| 285 |
*Effects:* Inserts a copy of `x` after `position`.
|
| 286 |
|
| 287 |
*Returns:* An iterator pointing to the copy of `x`.
|
| 288 |
|
| 289 |
``` cpp
|
| 290 |
iterator insert_after(const_iterator position, size_type n, const T& x);
|
| 291 |
```
|
| 292 |
|
| 293 |
-
*
|
| 294 |
iterator in the range \[`begin()`, `end()`).
|
| 295 |
|
| 296 |
*Effects:* Inserts `n` copies of `x` after `position`.
|
| 297 |
|
| 298 |
*Returns:* An iterator pointing to the last inserted copy of `x` or
|
|
@@ -301,13 +282,13 @@ iterator in the range \[`begin()`, `end()`).
|
|
| 301 |
``` cpp
|
| 302 |
template<class InputIterator>
|
| 303 |
iterator insert_after(const_iterator position, InputIterator first, InputIterator last);
|
| 304 |
```
|
| 305 |
|
| 306 |
-
*
|
| 307 |
-
iterator in the range \[`begin()`, `end()`). `first`
|
| 308 |
-
iterators in `*this`.
|
| 309 |
|
| 310 |
*Effects:* Inserts copies of elements in \[`first`, `last`) after
|
| 311 |
`position`.
|
| 312 |
|
| 313 |
*Returns:* An iterator pointing to the last inserted element or
|
|
@@ -325,11 +306,11 @@ iterator insert_after(const_iterator position, initializer_list<T> il);
|
|
| 325 |
``` cpp
|
| 326 |
template<class... Args>
|
| 327 |
iterator emplace_after(const_iterator position, Args&&... args);
|
| 328 |
```
|
| 329 |
|
| 330 |
-
*
|
| 331 |
iterator in the range \[`begin()`, `end()`).
|
| 332 |
|
| 333 |
*Effects:* Inserts an object of type `value_type` constructed with
|
| 334 |
`value_type(std::forward<Args>(args)...)` after `position`.
|
| 335 |
|
|
@@ -337,11 +318,11 @@ iterator in the range \[`begin()`, `end()`).
|
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
iterator erase_after(const_iterator position);
|
| 340 |
```
|
| 341 |
|
| 342 |
-
*
|
| 343 |
|
| 344 |
*Effects:* Erases the element pointed to by the iterator following
|
| 345 |
`position`.
|
| 346 |
|
| 347 |
*Returns:* An iterator pointing to the element following the one that
|
|
@@ -351,11 +332,11 @@ was erased, or `end()` if no such element exists.
|
|
| 351 |
|
| 352 |
``` cpp
|
| 353 |
iterator erase_after(const_iterator position, const_iterator last);
|
| 354 |
```
|
| 355 |
|
| 356 |
-
*
|
| 357 |
dereferenceable.
|
| 358 |
|
| 359 |
*Effects:* Erases the elements in the range (`position`, `last`).
|
| 360 |
|
| 361 |
*Returns:* `last`.
|
|
@@ -364,46 +345,52 @@ dereferenceable.
|
|
| 364 |
|
| 365 |
``` cpp
|
| 366 |
void resize(size_type sz);
|
| 367 |
```
|
| 368 |
|
|
|
|
|
|
|
| 369 |
*Effects:* If `sz < distance(begin(), end())`, erases the last
|
| 370 |
`distance(begin(), end()) - sz` elements from the list. Otherwise,
|
| 371 |
inserts `sz - distance(begin(), end())` default-inserted elements at the
|
| 372 |
end of the list.
|
| 373 |
|
| 374 |
-
*Requires:* `T` shall be `DefaultInsertable` into `*this`.
|
| 375 |
-
|
| 376 |
``` cpp
|
| 377 |
void resize(size_type sz, const value_type& c);
|
| 378 |
```
|
| 379 |
|
|
|
|
|
|
|
| 380 |
*Effects:* If `sz < distance(begin(), end())`, erases the last
|
| 381 |
`distance(begin(), end()) - sz` elements from the list. Otherwise,
|
| 382 |
inserts `sz - distance(begin(), end())` copies of `c` at the end of the
|
| 383 |
list.
|
| 384 |
|
| 385 |
-
*Requires:* `T` shall be `CopyInsertable` into `*this`.
|
| 386 |
-
|
| 387 |
``` cpp
|
| 388 |
void clear() noexcept;
|
| 389 |
```
|
| 390 |
|
| 391 |
*Effects:* Erases all elements in the range \[`begin()`, `end()`).
|
| 392 |
|
| 393 |
*Remarks:* Does not invalidate past-the-end iterators.
|
| 394 |
|
| 395 |
-
####
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
``` cpp
|
| 398 |
void splice_after(const_iterator position, forward_list& x);
|
| 399 |
void splice_after(const_iterator position, forward_list&& x);
|
| 400 |
```
|
| 401 |
|
| 402 |
-
*
|
| 403 |
iterator in the range \[`begin()`, `end()`).
|
| 404 |
-
`get_allocator() == x.get_allocator()`. `
|
|
|
|
| 405 |
|
| 406 |
*Effects:* Inserts the contents of `x` after `position`, and `x` becomes
|
| 407 |
empty. Pointers and references to the moved elements of `x` now refer to
|
| 408 |
those same elements but as members of `*this`. Iterators referring to
|
| 409 |
the moved elements will continue to refer to their elements, but they
|
|
@@ -416,14 +403,14 @@ now behave as iterators into `*this`, not into `x`.
|
|
| 416 |
``` cpp
|
| 417 |
void splice_after(const_iterator position, forward_list& x, const_iterator i);
|
| 418 |
void splice_after(const_iterator position, forward_list&& x, const_iterator i);
|
| 419 |
```
|
| 420 |
|
| 421 |
-
*
|
| 422 |
iterator in the range \[`begin()`, `end()`). The iterator following `i`
|
| 423 |
is a dereferenceable iterator in `x`.
|
| 424 |
-
`get_allocator() == x.get_allocator()`.
|
| 425 |
|
| 426 |
*Effects:* Inserts the element following `i` into `*this`, following
|
| 427 |
`position`, and removes it from `x`. The result is unchanged if
|
| 428 |
`position == i` or `position == ++i`. Pointers and references to `*++i`
|
| 429 |
continue to refer to the same element but as a member of `*this`.
|
|
@@ -439,15 +426,15 @@ void splice_after(const_iterator position, forward_list& x,
|
|
| 439 |
const_iterator first, const_iterator last);
|
| 440 |
void splice_after(const_iterator position, forward_list&& x,
|
| 441 |
const_iterator first, const_iterator last);
|
| 442 |
```
|
| 443 |
|
| 444 |
-
*
|
| 445 |
iterator in the range \[`begin()`, `end()`). (`first`, `last`) is a
|
| 446 |
valid range in `x`, and all iterators in the range (`first`, `last`) are
|
| 447 |
dereferenceable. `position` is not an iterator in the range (`first`,
|
| 448 |
-
`last`). `get_allocator() == x.get_allocator()`.
|
| 449 |
|
| 450 |
*Effects:* Inserts elements in the range (`first`, `last`) after
|
| 451 |
`position` and removes the elements from `x`. Pointers and references to
|
| 452 |
the moved elements of `x` now refer to those same elements but as
|
| 453 |
members of `*this`. Iterators referring to the moved elements will
|
|
@@ -455,39 +442,43 @@ continue to refer to their elements, but they now behave as iterators
|
|
| 455 |
into `*this`, not into `x`.
|
| 456 |
|
| 457 |
*Complexity:* 𝑂(`distance(first, last)`)
|
| 458 |
|
| 459 |
``` cpp
|
| 460 |
-
|
| 461 |
-
template
|
| 462 |
```
|
| 463 |
|
| 464 |
-
*Effects:* Erases all the elements in the list referred by a list
|
| 465 |
iterator `i` for which the following conditions hold: `*i == value` (for
|
| 466 |
`remove()`), `pred(*i)` is `true` (for `remove_if()`). Invalidates only
|
| 467 |
the iterators and references to the erased elements.
|
| 468 |
|
|
|
|
|
|
|
| 469 |
*Throws:* Nothing unless an exception is thrown by the equality
|
| 470 |
comparison or the predicate.
|
| 471 |
|
| 472 |
-
*Remarks:* Stable
|
| 473 |
|
| 474 |
*Complexity:* Exactly `distance(begin(), end())` applications of the
|
| 475 |
corresponding predicate.
|
| 476 |
|
| 477 |
``` cpp
|
| 478 |
-
|
| 479 |
-
template
|
| 480 |
```
|
| 481 |
|
| 482 |
*Effects:* Erases all but the first element from every consecutive group
|
| 483 |
of equal elements referred to by the iterator `i` in the range
|
| 484 |
\[`first + 1`, `last`) for which `*i == *(i-1)` (for the version with no
|
| 485 |
arguments) or `pred(*i, *(i - 1))` (for the version with a predicate
|
| 486 |
argument) holds. Invalidates only the iterators and references to the
|
| 487 |
erased elements.
|
| 488 |
|
|
|
|
|
|
|
| 489 |
*Throws:* Nothing unless an exception is thrown by the equality
|
| 490 |
comparison or the predicate.
|
| 491 |
|
| 492 |
*Complexity:* If the range \[`first`, `last`) is not empty, exactly
|
| 493 |
`(last - first) - 1` applications of the corresponding predicate,
|
|
@@ -498,44 +489,40 @@ void merge(forward_list& x);
|
|
| 498 |
void merge(forward_list&& x);
|
| 499 |
template<class Compare> void merge(forward_list& x, Compare comp);
|
| 500 |
template<class Compare> void merge(forward_list&& x, Compare comp);
|
| 501 |
```
|
| 502 |
|
| 503 |
-
*
|
| 504 |
-
|
| 505 |
-
`get_allocator() == x.get_allocator()`
|
|
|
|
| 506 |
|
| 507 |
*Effects:* Merges the two sorted ranges `[begin(), end())` and
|
| 508 |
`[x.begin(), x.end())`. `x` is empty after the merge. If an exception is
|
| 509 |
thrown other than by a comparison there are no effects. Pointers and
|
| 510 |
references to the moved elements of `x` now refer to those same elements
|
| 511 |
but as members of `*this`. Iterators referring to the moved elements
|
| 512 |
will continue to refer to their elements, but they now behave as
|
| 513 |
iterators into `*this`, not into `x`.
|
| 514 |
|
| 515 |
-
*Remarks:* Stable
|
| 516 |
-
`get_allocator() != x.get_allocator()`.
|
| 517 |
|
| 518 |
*Complexity:* At most
|
| 519 |
`distance(begin(), end()) + distance(x.begin(), x.end()) - 1`
|
| 520 |
comparisons.
|
| 521 |
|
| 522 |
``` cpp
|
| 523 |
void sort();
|
| 524 |
template<class Compare> void sort(Compare comp);
|
| 525 |
```
|
| 526 |
|
| 527 |
-
*Requires:* `operator<` (for the version with no arguments) or `comp`
|
| 528 |
-
(for the version with a comparison argument) defines a strict weak
|
| 529 |
-
ordering ([[alg.sorting]]).
|
| 530 |
-
|
| 531 |
*Effects:* Sorts the list according to the `operator<` or the `comp`
|
| 532 |
function object. If an exception is thrown, the order of the elements in
|
| 533 |
`*this` is unspecified. Does not affect the validity of iterators and
|
| 534 |
references.
|
| 535 |
|
| 536 |
-
*Remarks:* Stable
|
| 537 |
|
| 538 |
*Complexity:* Approximately N log N comparisons, where N is
|
| 539 |
`distance(begin(), end())`.
|
| 540 |
|
| 541 |
``` cpp
|
|
@@ -545,15 +532,24 @@ void reverse() noexcept;
|
|
| 545 |
*Effects:* Reverses the order of the elements in the list. Does not
|
| 546 |
affect the validity of iterators and references.
|
| 547 |
|
| 548 |
*Complexity:* Linear time.
|
| 549 |
|
| 550 |
-
####
|
| 551 |
|
| 552 |
``` cpp
|
| 553 |
-
template
|
| 554 |
-
|
| 555 |
-
|
| 556 |
```
|
| 557 |
|
| 558 |
-
*Effects:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
|
|
|
|
| 1 |
### Class template `forward_list` <a id="forwardlist">[[forwardlist]]</a>
|
| 2 |
|
| 3 |
+
#### Overview <a id="forwardlist.overview">[[forwardlist.overview]]</a>
|
| 4 |
|
| 5 |
A `forward_list` is a container that supports forward iterators and
|
| 6 |
allows constant time insert and erase operations anywhere within the
|
| 7 |
sequence, with storage management handled automatically. Fast random
|
| 8 |
access to list elements is not supported.
|
| 9 |
|
| 10 |
[*Note 1*: It is intended that `forward_list` have zero space or time
|
| 11 |
overhead relative to a hand-written C-style singly linked list. Features
|
| 12 |
that would conflict with that goal have been omitted. — *end note*]
|
| 13 |
|
| 14 |
+
A `forward_list` meets all of the requirements of a container (
|
| 15 |
+
[[container.req]]), except that the `size()` member function is not
|
| 16 |
+
provided and `operator==` has linear complexity. A `forward_list` also
|
| 17 |
+
meets all of the requirements for an allocator-aware container (
|
| 18 |
+
[[container.alloc.req]]). In addition, a `forward_list` provides the
|
| 19 |
+
`assign` member functions ([[container.seq.req]]) and several of the
|
| 20 |
+
optional container requirements ([[container.seq.opt]]). Descriptions
|
| 21 |
+
are provided here only for operations on `forward_list` that are not
|
| 22 |
+
described in that table or for operations where there is additional
|
| 23 |
+
semantic information.
|
|
|
|
| 24 |
|
| 25 |
[*Note 2*: Modifying any list requires access to the element preceding
|
| 26 |
the first element of interest, but in a `forward_list` there is no
|
| 27 |
constant-time way to access a preceding element. For this reason, ranges
|
| 28 |
that are modified, such as those supplied to `erase` and `splice`, must
|
|
|
|
| 31 |
``` cpp
|
| 32 |
namespace std {
|
| 33 |
template<class T, class Allocator = allocator<T>>
|
| 34 |
class forward_list {
|
| 35 |
public:
|
| 36 |
+
// types
|
| 37 |
using value_type = T;
|
| 38 |
using allocator_type = Allocator;
|
| 39 |
using pointer = typename allocator_traits<Allocator>::pointer;
|
| 40 |
using const_pointer = typename allocator_traits<Allocator>::const_pointer;
|
| 41 |
using reference = value_type&;
|
|
|
|
| 47 |
|
| 48 |
// [forwardlist.cons], construct/copy/destroy
|
| 49 |
forward_list() : forward_list(Allocator()) { }
|
| 50 |
explicit forward_list(const Allocator&);
|
| 51 |
explicit forward_list(size_type n, const Allocator& = Allocator());
|
| 52 |
+
forward_list(size_type n, const T& value, const Allocator& = Allocator());
|
|
|
|
| 53 |
template<class InputIterator>
|
| 54 |
+
forward_list(InputIterator first, InputIterator last, const Allocator& = Allocator());
|
|
|
|
| 55 |
forward_list(const forward_list& x);
|
| 56 |
forward_list(forward_list&& x);
|
| 57 |
forward_list(const forward_list& x, const Allocator&);
|
| 58 |
forward_list(forward_list&& x, const Allocator&);
|
| 59 |
forward_list(initializer_list<T>, const Allocator& = Allocator());
|
|
|
|
| 78 |
|
| 79 |
const_iterator cbegin() const noexcept;
|
| 80 |
const_iterator cbefore_begin() const noexcept;
|
| 81 |
const_iterator cend() const noexcept;
|
| 82 |
|
| 83 |
+
// capacity
|
| 84 |
+
[[nodiscard]] bool empty() const noexcept;
|
| 85 |
size_type max_size() const noexcept;
|
| 86 |
|
| 87 |
// [forwardlist.access], element access
|
| 88 |
reference front();
|
| 89 |
const_reference front() const;
|
|
|
|
| 113 |
void clear() noexcept;
|
| 114 |
|
| 115 |
// [forwardlist.ops], forward_list operations
|
| 116 |
void splice_after(const_iterator position, forward_list& x);
|
| 117 |
void splice_after(const_iterator position, forward_list&& x);
|
| 118 |
+
void splice_after(const_iterator position, forward_list& x, const_iterator i);
|
| 119 |
+
void splice_after(const_iterator position, forward_list&& x, const_iterator i);
|
|
|
|
|
|
|
| 120 |
void splice_after(const_iterator position, forward_list& x,
|
| 121 |
const_iterator first, const_iterator last);
|
| 122 |
void splice_after(const_iterator position, forward_list&& x,
|
| 123 |
const_iterator first, const_iterator last);
|
| 124 |
|
| 125 |
+
size_type remove(const T& value);
|
| 126 |
+
template<class Predicate> size_type remove_if(Predicate pred);
|
| 127 |
|
| 128 |
+
size_type unique();
|
| 129 |
+
template<class BinaryPredicate> size_type unique(BinaryPredicate binary_pred);
|
| 130 |
|
| 131 |
void merge(forward_list& x);
|
| 132 |
void merge(forward_list&& x);
|
| 133 |
template<class Compare> void merge(forward_list& x, Compare comp);
|
| 134 |
template<class Compare> void merge(forward_list&& x, Compare comp);
|
|
|
|
| 137 |
template<class Compare> void sort(Compare comp);
|
| 138 |
|
| 139 |
void reverse() noexcept;
|
| 140 |
};
|
| 141 |
|
| 142 |
+
template<class InputIterator, class Allocator = allocator<iter-value-type<InputIterator>>>
|
|
|
|
| 143 |
forward_list(InputIterator, InputIterator, Allocator = Allocator())
|
| 144 |
+
-> forward_list<iter-value-type<InputIterator>, Allocator>;
|
| 145 |
|
| 146 |
+
// swap
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
template<class T, class Allocator>
|
| 148 |
void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
|
| 149 |
noexcept(noexcept(x.swap(y)));
|
| 150 |
}
|
| 151 |
```
|
| 152 |
|
| 153 |
An incomplete type `T` may be used when instantiating `forward_list` if
|
| 154 |
+
the allocator meets the allocator completeness requirements
|
| 155 |
+
[[allocator.requirements.completeness]]. `T` shall be complete before
|
| 156 |
any member of the resulting specialization of `forward_list` is
|
| 157 |
referenced.
|
| 158 |
|
| 159 |
+
#### Constructors, copy, and assignment <a id="forwardlist.cons">[[forwardlist.cons]]</a>
|
| 160 |
|
| 161 |
``` cpp
|
| 162 |
explicit forward_list(const Allocator&);
|
| 163 |
```
|
| 164 |
|
|
|
|
| 169 |
|
| 170 |
``` cpp
|
| 171 |
explicit forward_list(size_type n, const Allocator& = Allocator());
|
| 172 |
```
|
| 173 |
|
| 174 |
+
*Preconditions:* `T` is *Cpp17DefaultInsertable* into `*this`.
|
| 175 |
+
|
| 176 |
*Effects:* Constructs a `forward_list` object with `n` default-inserted
|
| 177 |
elements using the specified allocator.
|
| 178 |
|
|
|
|
|
|
|
| 179 |
*Complexity:* Linear in `n`.
|
| 180 |
|
| 181 |
``` cpp
|
| 182 |
forward_list(size_type n, const T& value, const Allocator& = Allocator());
|
| 183 |
```
|
| 184 |
|
| 185 |
+
*Preconditions:* `T` is *Cpp17CopyInsertable* into `*this`.
|
| 186 |
+
|
| 187 |
*Effects:* Constructs a `forward_list` object with `n` copies of `value`
|
| 188 |
using the specified allocator.
|
| 189 |
|
|
|
|
|
|
|
| 190 |
*Complexity:* Linear in `n`.
|
| 191 |
|
| 192 |
``` cpp
|
| 193 |
template<class InputIterator>
|
| 194 |
forward_list(InputIterator first, InputIterator last, const Allocator& = Allocator());
|
|
|
|
| 197 |
*Effects:* Constructs a `forward_list` object equal to the range
|
| 198 |
\[`first`, `last`).
|
| 199 |
|
| 200 |
*Complexity:* Linear in `distance(first, last)`.
|
| 201 |
|
| 202 |
+
#### Iterators <a id="forwardlist.iter">[[forwardlist.iter]]</a>
|
| 203 |
|
| 204 |
``` cpp
|
| 205 |
iterator before_begin() noexcept;
|
| 206 |
const_iterator before_begin() const noexcept;
|
| 207 |
const_iterator cbefore_begin() const noexcept;
|
|
|
|
| 213 |
*Effects:* `cbefore_begin()` is equivalent to
|
| 214 |
`const_cast<forward_list const&>(*this).before_begin()`.
|
| 215 |
|
| 216 |
*Remarks:* `before_begin() == end()` shall equal `false`.
|
| 217 |
|
| 218 |
+
#### Element access <a id="forwardlist.access">[[forwardlist.access]]</a>
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
reference front();
|
| 222 |
const_reference front() const;
|
| 223 |
```
|
| 224 |
|
| 225 |
*Returns:* `*begin()`
|
| 226 |
|
| 227 |
+
#### Modifiers <a id="forwardlist.modifiers">[[forwardlist.modifiers]]</a>
|
| 228 |
|
| 229 |
None of the overloads of `insert_after` shall affect the validity of
|
| 230 |
iterators and references, and `erase_after` shall invalidate only
|
| 231 |
iterators and references to the erased elements. If an exception is
|
| 232 |
thrown during `insert_after` there shall be no effect. Inserting `n`
|
|
|
|
| 258 |
``` cpp
|
| 259 |
iterator insert_after(const_iterator position, const T& x);
|
| 260 |
iterator insert_after(const_iterator position, T&& x);
|
| 261 |
```
|
| 262 |
|
| 263 |
+
*Preconditions:* `position` is `before_begin()` or is a dereferenceable
|
| 264 |
iterator in the range \[`begin()`, `end()`).
|
| 265 |
|
| 266 |
*Effects:* Inserts a copy of `x` after `position`.
|
| 267 |
|
| 268 |
*Returns:* An iterator pointing to the copy of `x`.
|
| 269 |
|
| 270 |
``` cpp
|
| 271 |
iterator insert_after(const_iterator position, size_type n, const T& x);
|
| 272 |
```
|
| 273 |
|
| 274 |
+
*Preconditions:* `position` is `before_begin()` or is a dereferenceable
|
| 275 |
iterator in the range \[`begin()`, `end()`).
|
| 276 |
|
| 277 |
*Effects:* Inserts `n` copies of `x` after `position`.
|
| 278 |
|
| 279 |
*Returns:* An iterator pointing to the last inserted copy of `x` or
|
|
|
|
| 282 |
``` cpp
|
| 283 |
template<class InputIterator>
|
| 284 |
iterator insert_after(const_iterator position, InputIterator first, InputIterator last);
|
| 285 |
```
|
| 286 |
|
| 287 |
+
*Preconditions:* `position` is `before_begin()` or is a dereferenceable
|
| 288 |
+
iterator in the range \[`begin()`, `end()`). Neither `first` nor `last`
|
| 289 |
+
are iterators in `*this`.
|
| 290 |
|
| 291 |
*Effects:* Inserts copies of elements in \[`first`, `last`) after
|
| 292 |
`position`.
|
| 293 |
|
| 294 |
*Returns:* An iterator pointing to the last inserted element or
|
|
|
|
| 306 |
``` cpp
|
| 307 |
template<class... Args>
|
| 308 |
iterator emplace_after(const_iterator position, Args&&... args);
|
| 309 |
```
|
| 310 |
|
| 311 |
+
*Preconditions:* `position` is `before_begin()` or is a dereferenceable
|
| 312 |
iterator in the range \[`begin()`, `end()`).
|
| 313 |
|
| 314 |
*Effects:* Inserts an object of type `value_type` constructed with
|
| 315 |
`value_type(std::forward<Args>(args)...)` after `position`.
|
| 316 |
|
|
|
|
| 318 |
|
| 319 |
``` cpp
|
| 320 |
iterator erase_after(const_iterator position);
|
| 321 |
```
|
| 322 |
|
| 323 |
+
*Preconditions:* The iterator following `position` is dereferenceable.
|
| 324 |
|
| 325 |
*Effects:* Erases the element pointed to by the iterator following
|
| 326 |
`position`.
|
| 327 |
|
| 328 |
*Returns:* An iterator pointing to the element following the one that
|
|
|
|
| 332 |
|
| 333 |
``` cpp
|
| 334 |
iterator erase_after(const_iterator position, const_iterator last);
|
| 335 |
```
|
| 336 |
|
| 337 |
+
*Preconditions:* All iterators in the range (`position`, `last`) are
|
| 338 |
dereferenceable.
|
| 339 |
|
| 340 |
*Effects:* Erases the elements in the range (`position`, `last`).
|
| 341 |
|
| 342 |
*Returns:* `last`.
|
|
|
|
| 345 |
|
| 346 |
``` cpp
|
| 347 |
void resize(size_type sz);
|
| 348 |
```
|
| 349 |
|
| 350 |
+
*Preconditions:* `T` is *Cpp17DefaultInsertable* into `*this`.
|
| 351 |
+
|
| 352 |
*Effects:* If `sz < distance(begin(), end())`, erases the last
|
| 353 |
`distance(begin(), end()) - sz` elements from the list. Otherwise,
|
| 354 |
inserts `sz - distance(begin(), end())` default-inserted elements at the
|
| 355 |
end of the list.
|
| 356 |
|
|
|
|
|
|
|
| 357 |
``` cpp
|
| 358 |
void resize(size_type sz, const value_type& c);
|
| 359 |
```
|
| 360 |
|
| 361 |
+
*Preconditions:* `T` is *Cpp17CopyInsertable* into `*this`.
|
| 362 |
+
|
| 363 |
*Effects:* If `sz < distance(begin(), end())`, erases the last
|
| 364 |
`distance(begin(), end()) - sz` elements from the list. Otherwise,
|
| 365 |
inserts `sz - distance(begin(), end())` copies of `c` at the end of the
|
| 366 |
list.
|
| 367 |
|
|
|
|
|
|
|
| 368 |
``` cpp
|
| 369 |
void clear() noexcept;
|
| 370 |
```
|
| 371 |
|
| 372 |
*Effects:* Erases all elements in the range \[`begin()`, `end()`).
|
| 373 |
|
| 374 |
*Remarks:* Does not invalidate past-the-end iterators.
|
| 375 |
|
| 376 |
+
#### Operations <a id="forwardlist.ops">[[forwardlist.ops]]</a>
|
| 377 |
+
|
| 378 |
+
In this subclause, arguments for a template parameter named `Predicate`
|
| 379 |
+
or `BinaryPredicate` shall meet the corresponding requirements in
|
| 380 |
+
[[algorithms.requirements]]. For `merge` and `sort`, the definitions and
|
| 381 |
+
requirements in [[alg.sorting]] apply.
|
| 382 |
|
| 383 |
``` cpp
|
| 384 |
void splice_after(const_iterator position, forward_list& x);
|
| 385 |
void splice_after(const_iterator position, forward_list&& x);
|
| 386 |
```
|
| 387 |
|
| 388 |
+
*Preconditions:* `position` is `before_begin()` or is a dereferenceable
|
| 389 |
iterator in the range \[`begin()`, `end()`).
|
| 390 |
+
`get_allocator() == x.get_allocator()` is `true`. `addressof(x) != this`
|
| 391 |
+
is `true`.
|
| 392 |
|
| 393 |
*Effects:* Inserts the contents of `x` after `position`, and `x` becomes
|
| 394 |
empty. Pointers and references to the moved elements of `x` now refer to
|
| 395 |
those same elements but as members of `*this`. Iterators referring to
|
| 396 |
the moved elements will continue to refer to their elements, but they
|
|
|
|
| 403 |
``` cpp
|
| 404 |
void splice_after(const_iterator position, forward_list& x, const_iterator i);
|
| 405 |
void splice_after(const_iterator position, forward_list&& x, const_iterator i);
|
| 406 |
```
|
| 407 |
|
| 408 |
+
*Preconditions:* `position` is `before_begin()` or is a dereferenceable
|
| 409 |
iterator in the range \[`begin()`, `end()`). The iterator following `i`
|
| 410 |
is a dereferenceable iterator in `x`.
|
| 411 |
+
`get_allocator() == x.get_allocator()` is `true`.
|
| 412 |
|
| 413 |
*Effects:* Inserts the element following `i` into `*this`, following
|
| 414 |
`position`, and removes it from `x`. The result is unchanged if
|
| 415 |
`position == i` or `position == ++i`. Pointers and references to `*++i`
|
| 416 |
continue to refer to the same element but as a member of `*this`.
|
|
|
|
| 426 |
const_iterator first, const_iterator last);
|
| 427 |
void splice_after(const_iterator position, forward_list&& x,
|
| 428 |
const_iterator first, const_iterator last);
|
| 429 |
```
|
| 430 |
|
| 431 |
+
*Preconditions:* `position` is `before_begin()` or is a dereferenceable
|
| 432 |
iterator in the range \[`begin()`, `end()`). (`first`, `last`) is a
|
| 433 |
valid range in `x`, and all iterators in the range (`first`, `last`) are
|
| 434 |
dereferenceable. `position` is not an iterator in the range (`first`,
|
| 435 |
+
`last`). `get_allocator() == x.get_allocator()` is `true`.
|
| 436 |
|
| 437 |
*Effects:* Inserts elements in the range (`first`, `last`) after
|
| 438 |
`position` and removes the elements from `x`. Pointers and references to
|
| 439 |
the moved elements of `x` now refer to those same elements but as
|
| 440 |
members of `*this`. Iterators referring to the moved elements will
|
|
|
|
| 442 |
into `*this`, not into `x`.
|
| 443 |
|
| 444 |
*Complexity:* 𝑂(`distance(first, last)`)
|
| 445 |
|
| 446 |
``` cpp
|
| 447 |
+
size_type remove(const T& value);
|
| 448 |
+
template<class Predicate> size_type remove_if(Predicate pred);
|
| 449 |
```
|
| 450 |
|
| 451 |
+
*Effects:* Erases all the elements in the list referred to by a list
|
| 452 |
iterator `i` for which the following conditions hold: `*i == value` (for
|
| 453 |
`remove()`), `pred(*i)` is `true` (for `remove_if()`). Invalidates only
|
| 454 |
the iterators and references to the erased elements.
|
| 455 |
|
| 456 |
+
*Returns:* The number of elements erased.
|
| 457 |
+
|
| 458 |
*Throws:* Nothing unless an exception is thrown by the equality
|
| 459 |
comparison or the predicate.
|
| 460 |
|
| 461 |
+
*Remarks:* Stable [[algorithm.stable]].
|
| 462 |
|
| 463 |
*Complexity:* Exactly `distance(begin(), end())` applications of the
|
| 464 |
corresponding predicate.
|
| 465 |
|
| 466 |
``` cpp
|
| 467 |
+
size_type unique();
|
| 468 |
+
template<class BinaryPredicate> size_type unique(BinaryPredicate pred);
|
| 469 |
```
|
| 470 |
|
| 471 |
*Effects:* Erases all but the first element from every consecutive group
|
| 472 |
of equal elements referred to by the iterator `i` in the range
|
| 473 |
\[`first + 1`, `last`) for which `*i == *(i-1)` (for the version with no
|
| 474 |
arguments) or `pred(*i, *(i - 1))` (for the version with a predicate
|
| 475 |
argument) holds. Invalidates only the iterators and references to the
|
| 476 |
erased elements.
|
| 477 |
|
| 478 |
+
*Returns:* The number of elements erased.
|
| 479 |
+
|
| 480 |
*Throws:* Nothing unless an exception is thrown by the equality
|
| 481 |
comparison or the predicate.
|
| 482 |
|
| 483 |
*Complexity:* If the range \[`first`, `last`) is not empty, exactly
|
| 484 |
`(last - first) - 1` applications of the corresponding predicate,
|
|
|
|
| 489 |
void merge(forward_list&& x);
|
| 490 |
template<class Compare> void merge(forward_list& x, Compare comp);
|
| 491 |
template<class Compare> void merge(forward_list&& x, Compare comp);
|
| 492 |
```
|
| 493 |
|
| 494 |
+
*Preconditions:* `*this` and `x` are both sorted with respect to the
|
| 495 |
+
comparator `operator<` (for the first two overloads) or `comp` (for the
|
| 496 |
+
last two overloads), and `get_allocator() == x.get_allocator()` is
|
| 497 |
+
`true`.
|
| 498 |
|
| 499 |
*Effects:* Merges the two sorted ranges `[begin(), end())` and
|
| 500 |
`[x.begin(), x.end())`. `x` is empty after the merge. If an exception is
|
| 501 |
thrown other than by a comparison there are no effects. Pointers and
|
| 502 |
references to the moved elements of `x` now refer to those same elements
|
| 503 |
but as members of `*this`. Iterators referring to the moved elements
|
| 504 |
will continue to refer to their elements, but they now behave as
|
| 505 |
iterators into `*this`, not into `x`.
|
| 506 |
|
| 507 |
+
*Remarks:* Stable [[algorithm.stable]].
|
|
|
|
| 508 |
|
| 509 |
*Complexity:* At most
|
| 510 |
`distance(begin(), end()) + distance(x.begin(), x.end()) - 1`
|
| 511 |
comparisons.
|
| 512 |
|
| 513 |
``` cpp
|
| 514 |
void sort();
|
| 515 |
template<class Compare> void sort(Compare comp);
|
| 516 |
```
|
| 517 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
*Effects:* Sorts the list according to the `operator<` or the `comp`
|
| 519 |
function object. If an exception is thrown, the order of the elements in
|
| 520 |
`*this` is unspecified. Does not affect the validity of iterators and
|
| 521 |
references.
|
| 522 |
|
| 523 |
+
*Remarks:* Stable [[algorithm.stable]].
|
| 524 |
|
| 525 |
*Complexity:* Approximately N log N comparisons, where N is
|
| 526 |
`distance(begin(), end())`.
|
| 527 |
|
| 528 |
``` cpp
|
|
|
|
| 532 |
*Effects:* Reverses the order of the elements in the list. Does not
|
| 533 |
affect the validity of iterators and references.
|
| 534 |
|
| 535 |
*Complexity:* Linear time.
|
| 536 |
|
| 537 |
+
#### Erasure <a id="forward.list.erasure">[[forward.list.erasure]]</a>
|
| 538 |
|
| 539 |
``` cpp
|
| 540 |
+
template<class T, class Allocator, class U>
|
| 541 |
+
typename forward_list<T, Allocator>::size_type
|
| 542 |
+
erase(forward_list<T, Allocator>& c, const U& value);
|
| 543 |
```
|
| 544 |
|
| 545 |
+
*Effects:* Equivalent to:
|
| 546 |
+
`return erase_if(c, [&](auto& elem) { return elem == value; });`
|
| 547 |
+
|
| 548 |
+
``` cpp
|
| 549 |
+
template<class T, class Allocator, class Predicate>
|
| 550 |
+
typename forward_list<T, Allocator>::size_type
|
| 551 |
+
erase_if(forward_list<T, Allocator>& c, Predicate pred);
|
| 552 |
+
```
|
| 553 |
+
|
| 554 |
+
*Effects:* Equivalent to: `return c.remove_if(pred);`
|
| 555 |
|