Difference between revisions of "cpp/named req/Container"
From cppreference.com
m (→Other requirements: fmt) |
m (fmt; Capitalize 1st letters; rm <ref> duplicate for std::array) |
||
Line 20: | Line 20: | ||
|{{tt|const_reference}}||{{c/core|const T&}}|| | |{{tt|const_reference}}||{{c/core|const T&}}|| | ||
|- | |- | ||
− | |{{tt|iterator}}|| | + | |{{tt|iterator}}||Iterator whose [[cpp/iterator#Types and writability|value type]] is {{tt|T}}||{{named req|ForwardIterator}}<br>convertible to {{tt|const_iterator}} |
|- | |- | ||
− | |{{tt|const_iterator}}|| | + | |{{tt|const_iterator}}||Constant iterator whose [[cpp/iterator#Types and writability|value type]] is {{tt|T}}||{{named req|ForwardIterator}} |
|- | |- | ||
− | |{{tt|difference_type}}|| | + | |{{tt|difference_type}}||Signed integer |
− | | | + | |Must be the same as {{tt|[[cpp/iterator/iterator_traits|iterator_traits]]::difference_type}} for {{tt|iterator}} and {{tt|const_iterator}} |
|- | |- | ||
− | |{{tt|size_type}}|| | + | |{{tt|size_type}}||Unsigned integer||Large enough to represent all positive values of {{tt|difference_type}} |
|} | |} | ||
Line 34: | Line 34: | ||
!Expression||Return type||Semantics||Conditions||Complexity | !Expression||Return type||Semantics||Conditions||Complexity | ||
|- | |- | ||
− | |{{c|C()}}||{{tt|C}}|| | + | |{{c|C()}}||{{tt|C}}||Creates an empty container||Post: {{c|C().empty()}}<br>{{c|1=== true}}||Constant |
|- | |- | ||
− | |{{c|C(a)}}||{{tt|C}}|| | + | |{{c|C(a)}}||{{tt|C}}||Creates a copy of {{c|a}}||Pre: T must be {{named req|CopyInsertable}}<br/>Post: {{c|1=a == C(a)}}||Linear |
|- | |- | ||
− | |{{c|C(rv)}}<br>{{mark since c++11}}||C|| | + | |{{c|C(rv)}}<br>{{mark since c++11}}||{{tt|C}}||Moves {{c|rv}}||Post: equal to the value {{c|rv}} had before this construction||Constant<ref name="array">{{mark since c++11}} Linear for {{lc|std::array}}</ref> |
|- | |- | ||
− | |{{c|1=a = b}}||{{tt|C&}}|| | + | |{{c|1=a = b}}||{{tt|C&}}||Destroys or copy-assigns all elements of {{c|a}} from elements of {{c|b}}||Post: {{c|1=a == b}}||Linear |
|- | |- | ||
− | |{{c|1=a = rv}}<br>{{mark since c++11}}||{{tt|C&}}|| | + | |{{c|1=a = rv}}<br>{{mark since c++11}}||{{tt|C&}}||Destroys or move-assigns all elements of {{c|a}} from elements of {{c|rv}}||Post: if {{c|a}} and {{c|rv}} do not refer the same object, {{c|a}} is equal to the value {{c|rv}} had before this assignment||Linear |
|- | |- | ||
− | |{{c|a.~C()}}||{{c/core|void}}|| | + | |{{c|a.~C()}}||{{c/core|void}}||Destroys all elements of {{c|a}} and frees all memory||||Linear |
|- | |- | ||
− | |{{c|a.begin()}}||{{tt|(const_)iterator}}||Iterator to the first element of {{c|a}}|| ||Constant | + | |{{c|a.begin()}}||{{tt|(const_)iterator}}||Iterator to the first element of {{c|a}}||||Constant |
|- | |- | ||
− | |{{c|a.end()}}||{{tt|(const_)iterator}}||Iterator to one past the last element of {{c|a}}|| | + | |{{c|a.end()}}||{{tt|(const_)iterator}}||Iterator to one past the last element of {{c|a}}||||Constant |
|- | |- | ||
− | |{{c|a.cbegin()}}<br>{{mark since c++11}}||{{tt|const_iterator}}||{{c|const_cast<const C&>(a).begin()}}|| ||Constant | + | |{{c|a.cbegin()}}<br>{{mark since c++11}}||{{tt|const_iterator}}||{{c|const_cast<const C&>(a).begin()}}||||Constant |
|- | |- | ||
− | |{{c|a.cend()}}<br>{{mark since c++11}}||{{tt|const_iterator}}||{{c|const_cast<const C&>(a).end()}}|| ||Constant | + | |{{c|a.cend()}}<br>{{mark since c++11}}||{{tt|const_iterator}}||{{c|const_cast<const C&>(a).end()}}||||Constant |
|- | |- | ||
− | |{{c|1=a == b}} || | + | |{{c|1=a == b}}||Convertible to {{c/core|bool}}||{{rev begin}} |
{{rev|until=c++14|{{c|1=a.size() == b.size() && | {{rev|until=c++14|{{c|1=a.size() == b.size() && | ||
std::equal(a.begin(), | std::equal(a.begin(), | ||
Line 61: | Line 61: | ||
b.begin(), b.end())}}}} | b.begin(), b.end())}}}} | ||
{{rev end}} | {{rev end}} | ||
− | || Pre: {{tt|T}} must be {{named req|EqualityComparable}} || Constant<ref> | + | ||Pre: {{tt|T}} must be {{named req|EqualityComparable}}||Constant<ref>Always linear for {{lc|std::forward_list}}</ref> if {{c|1=a.size() !=}}<br>{{c|b.size()}}, linear otherwise<!-- LWG2257 --> |
|- | |- | ||
− | |{{c|1=a != b}} || convertible to {{c/core|bool}} || {{c|1=!(a == b)}} || || Linear | + | |{{c|1=a != b}}||convertible to {{c/core|bool}}||{{c|1=!(a == b)}}||||Linear |
|- | |- | ||
− | |{{c|a.swap(b)}} || {{c/core|void}} || | + | |{{c|a.swap(b)}}||{{c/core|void}}||Exchanges the values of {{c|a}} and {{c|b}}|| |
− | | Constant<ref name="array" | + | |Constant<ref name="array"/><ref name="should">{{mark until c++11}} Not strictly constant</ref> |
|- | |- | ||
− | |{{c|swap(a, b)}} || {{c/core|void}} || {{c|a.swap(b)}} || || Constant<ref name="array"/> | + | |{{c|swap(a, b)}}||{{c/core|void}}||{{c|a.swap(b)}}||||Constant<ref name="array"/> |
|- | |- | ||
− | |{{c|a.size()}} || {{tt|size_type}} || {{c|std::distance(a.begin(), a.end())}} || || Constant<ref name="should" /> | + | |{{c|a.size()}}||{{tt|size_type}}||{{c|std::distance(a.begin(), a.end())}}||||Constant<ref name="should"/> |
|- | |- | ||
− | |{{c|a.max_size()}} || {{tt|size_type}} || {{c|b.size()}} where {{c|b}} is the largest possible container || || Constant<ref name="should" /> | + | |{{c|a.max_size()}}||{{tt|size_type}}||{{c|b.size()}} where {{c|b}} is the largest possible container||||Constant<ref name="should"/> |
|- | |- | ||
− | |{{c|a.empty()}} || | + | |{{c|a.empty()}}||Convertible to {{c/core|bool}}||{{c|1=a.begin() == a.end()}}||||Constant |
|- | |- | ||
− | !colspan=6| | + | !colspan=6|Notes |
|- | |- | ||
|colspan=6|<references/> | |colspan=6|<references/> | ||
Line 106: | Line 106: | ||
{{dr list item|wg=lwg|dr=774|std=C++98|before=there was no requirement on {{c|swap(a, b)}}|after=added}} | {{dr list item|wg=lwg|dr=774|std=C++98|before=there was no requirement on {{c|swap(a, b)}}|after=added}} | ||
{{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 | + | {{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 unspecified}} |
{{dr list end}} | {{dr list end}} | ||
Revision as of 04:12, 6 July 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
(Container)
T
(Type)
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 774 | C++98 | there was no requirement on swap(a, b) | added |
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 unspecified |
See also
C++ documentation for Containers library
|