|
|
|
AContainer is an object used to store other objects and taking care of the management of the memory used by the objects it contains.
Contents |
Given the following types and values:
Type | Definition |
T | an object type |
C | a container class containing objects of typeT |
Value | Definition |
u,v | values of typeC orconst C |
mv | a value of typeC |
cv | a value of typeconst C |
lhs,rhs | lvalues of typeC |
i,j | values of typeC::iterator orconst C::iterator |
C
satisfies the requirements ofContainer if the following types, statements, and expressions are well-formed and have the specified semantics:
Type | Definition | Requirements |
---|---|---|
typename C::value_type | T | T isCopyConstructible(until C++11)Erasable fromC (since C++11). |
typename C::reference | T& | No explicit requirement |
typename C::const_reference | const T& | |
typename C::iterator | an iterator type |
|
typename C::const_iterator | a constant iterator type | C::const_iterator is aLegacyForwardIterator, and itsvalue type isT . |
typename C::difference_type | a signed integer type | C::difference_type is the same as thedifference type ofC::iterator andC::const_iterator . |
typename C::size_type | an unsigned integer type | C::size_type is large enough to represent all non-negative values ofC::difference_type . |
Statement | Semantics | Complexity | |||
---|---|---|---|---|---|
C c; C c= C(); | Postcondition | c.empty() istrue. | constant | ||
C c(v); C c= C(v); | Precondition |
| linear[1] | ||
Postcondition |
| ||||
Notes | |||||
|
Expression | Type | Semantics | Complexity | |||||
---|---|---|---|---|---|---|---|---|
C() | C | Postcondition | C().empty() istrue. | constant | ||||
C(v) | C | Precondition |
| constant[1] | ||||
Postcondition |
| |||||||
lhs= v | C& | Postcondition |
| linear | ||||
v.~C() | void | Effect | Destroys all elements ofv and deallocates all memory obtained. | linear | ||||
mv.begin() | C::iterator | Effect | Returns an iterator pointing to the first element ofmv. | constant | ||||
cv.begin() | C::const_iterator | Effect | Returns an iterator pointing to the first element ofcv. | constant | ||||
mv.end() | C::iterator | Effect | Returns the past-the-end iterator ofmv. | constant | ||||
cv.end() | C::const_iterator | Effect | Returns the past-the-end iterator ofcv. | constant | ||||
v.cbegin() (since C++11) | C::const_iterator | Effect | Returnsconst_cast<const C&>(v).begin(). | constant | ||||
v.cend() (since C++11) | C::const_iterator | Effect | Returnsconst_cast<const C&>(v).end(). | constant | ||||
i<=> j (since C++20) | std::strong_ordering | Constraint | This expression is only required to be well-formed ifC::iterator satisfies the random access iterator requirements. | constant | ||||
u== v | bool | Effect | Returns
| linear[2] | ||||
u!= v | Effect | Equivalent to!(u== v). | ||||||
lhs.swap(rhs) swap(lhs, rhs) | void | Effect | Exchanges the contents oflhs andrhs. | constant[3] | ||||
v.size() | C::size_type | Effect | Returns the number of elements[4] ofv. | constant | ||||
v.max_size() | C::size_type | Effect | Returns the number of elements of the largest possible container of typeC . | constant | ||||
v.empty() | bool | Effect | Returnsv.begin()== v.end(). | constant | ||||
Optional container requirements (only provided for some types of containers) | ||||||||
u<=> v (since C++20) | synth-three-way-result <C::value_type> | Precondition | EitherT modelsthree_way_comparable , oroperator< is a total ordering relationship defined for values of typeT andconst T. | linear | ||||
Effect | Returnsstd::lexicographical_compare_three_way (u.begin(), u.end(), v.begin(), v.end(), synth-three-way )[5]. | |||||||
Notes | ||||||||
|
In the expressionsi== j,i!= j,i< j,i<= j,i>= j,i> j andi- j, ifi and/orj are replaced by iterators of typeC::const_iterator
pointing to the same element respectively, the semantics remain the same.
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 andconst_iterator types might be incomparable | required to be comparable |
LWG 276 | C++98 | T was required to beCopyAssignable | T is required to beCopyConstructible |
LWG 322 | C++98 | the value types ofiterator andconst_iterator were not specified | specified asT |
LWG 774 | C++98 | there was no requirement onswap(a, b) | added |
LWG 883 | C++98 | a.swap(b) was defined asswap(a, b), resulted in circular definition | defined as exchanging the values ofa andb |
LWG 1319 | C++98 | iterator andconst_iterator might not have multipass guarantee | they are required to satisfy the requirements of LegacyForwardIterator |
LWG 2114 (P2167R3) | C++98 | non-bool return types of some functions were allowed | disallowed |
LWG 2182 | C++98 | the types deonted byreference andconst_reference were poorly specified | improved wording |
LWG 2257 | C++98 | two containers required linear time to compare equal even if they have different sizes | only requires constant time in this case |
LWG 2263 | C++11 | the resolution ofLWG 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 |
N3346 | C++11 | C::value_type was required to beDestructible | required to beErasable fromC |
C++ documentation forContainers library |