Difference between revisions of "cpp/named req/Container"
From cppreference.com
(Added LWG issue #355 DR.) |
m (end iterator -> past-the-end iterator.) |
||
Line 104: | Line 104: | ||
{{dr list item|wg=lwg|dr=276|std=C++98|before={{tt|T}} was required to be {{named req|CopyAssignable}}|after={{tt|T}} is required to be {{named req|CopyConstructible}}}} | {{dr list item|wg=lwg|dr=276|std=C++98|before={{tt|T}} was required to be {{named req|CopyAssignable}}|after={{tt|T}} is required to be {{named req|CopyConstructible}}}} | ||
{{dr list item|wg=lwg|dr=322|std=C++98|before=the value types of {{tt|iterator}} and {{tt|const_iterator}} were not specified|after=specified as {{tt|T}}}} | {{dr list item|wg=lwg|dr=322|std=C++98|before=the value types of {{tt|iterator}} and {{tt|const_iterator}} were not specified|after=specified as {{tt|T}}}} | ||
− | {{dr list item|wg=lwg|dr=355|std=C++98|before=calling {{tt|back()}} or {{tt|pop_back()}}<!-- they are optionally required in old standards --> would<br>decrement the end iterator directly|after=decrement a copy of<br>the end iterator instead}} | + | {{dr list item|wg=lwg|dr=355|std=C++98|before=calling {{tt|back()}} or {{tt|pop_back()}}<!-- they are optionally required in old standards --> would<br>decrement the past-the-end iterator directly|after=decrement a copy of the<br>past-the-end iterator instead}} |
{{dr list item|wg=lwg|dr=2263|std=C++11|before=the resolution of {{lwg|179}} was accidentally dropped in C++11|after=restored}} | {{dr list item|wg=lwg|dr=2263|std=C++11|before=the resolution of {{lwg|179}} was accidentally dropped in C++11|after=restored}} | ||
{{dr list item|wg=lwg|dr=2839|std=C++11|before=self move assignment of standard containers was not allowed|after=allowed but the result is unspecifed}} | {{dr list item|wg=lwg|dr=2839|std=C++11|before=self move assignment of standard containers was not allowed|after=allowed but the result is unspecifed}} |
Revision as of 22:37, 16 January 2023
A Container is an object used to store other objects and taking care of the management of the memory used by the objects it contains.
Contents |
Requirements
-
T
, an element type; -
C
, a Container type containing elements of typeT
; - a and b, objects of type
C
; - rv, a prvalue expression of type
C
.
Types
Name | Type | Requirements |
---|---|---|
value_type |
T |
CopyConstructible(until C++11)Erasable(since C++11) |
reference |
T& |
|
const_reference |
const T& | |
iterator |
iterator whose value type is T |
LegacyForwardIterator convertible to const_iterator
|
const_iterator |
constant iterator whose value type is T |
LegacyForwardIterator |
difference_type |
signed integer | must be the same as iterator_traits::difference_type for iterator and const_iterator
|
size_type |
unsigned integer | large enough to represent all positive values of difference_type
|
Methods and operators
Expression | Return type | Semantics | Conditions | Complexity | ||||
---|---|---|---|---|---|---|---|---|
C() | C |
creates an empty container | Post: C().empty() == true |
Constant | ||||
C(a) | C |
creates a copy of a | Pre: T must be CopyInsertable Post: a == C(a) |
Linear | ||||
C(rv) (since C++11) |
C | moves rv | Post: equal to the value rv had before this construction | Constant[1] | ||||
a = b | C& |
destroys or copy-assigns all elements of a from elements of b | Post: a == b | Linear | ||||
a = rv (since C++11) |
C& |
destroys or move-assigns all elements of a from elements of rv | Post: if a and rv do not refer the same object, a is equal to the value rv had before this assignment | Linear | ||||
a.~C() | void | destroys all elements of a and frees all memory | Linear | |||||
a.begin() | (const_)iterator |
Iterator to the first element of a | Constant | |||||
a.end() | (const_)iterator |
Iterator to one past the last element of a | Constant | |||||
a.cbegin() (since C++11) |
const_iterator |
const_cast<const C&>(a).begin() | Constant | |||||
a.cend() (since C++11) |
const_iterator |
const_cast<const C&>(a).end() | Constant | |||||
a == b | convertible to bool |
|
Pre: T must be EqualityComparable |
Constant[2] if a.size() != b.size(), linear otherwise | ||||
a != b | convertible to bool | !(a == b) | Linear | |||||
a.swap(b) | void | exchanges the values of a and b | Constant[1][3] | |||||
swap(a, b) | void | a.swap(b) | Constant[1] | |||||
a.size() | size_type |
std::distance(a.begin(), a.end()) | Constant[3] | |||||
a.max_size() | size_type |
b.size() where b is the largest possible container | Constant[3] | |||||
a.empty() | convertible to bool | a.begin() == a.end() | Constant | |||||
notes | ||||||||
|
Given
-
i
andj
, objects of a container'siterator
type,
in the expressions i == j, i != j, i < j, i <= j, i >= j, i > j, i - j, either or both may be replaced by an object of the container's const_iterator
type referring to the same element with no change in semantics.
Container data races
Other requirements
- C
- T
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 179 | C++98 | iterator and const_iterator types might be incomparable
|
required to be comparable |
LWG 276 | C++98 | T was required to be CopyAssignable
|
T is required to be CopyConstructible
|
LWG 322 | C++98 | the value types of iterator and const_iterator were not specified
|
specified as T
|
LWG 355 | C++98 | calling back() or pop_back() woulddecrement the past-the-end iterator directly |
decrement a copy of the past-the-end iterator instead |
LWG 2263 | C++11 | the resolution of LWG issue 179 was accidentally dropped in C++11 | restored |
LWG 2839 | C++11 | self move assignment of standard containers was not allowed | allowed but the result is unspecifed |
See also
C++ documentation for Containers library
|