Container: Description
Container: Description
It supports insertion and removal of elements. Refinement of Forward Container, Default Constructible Associated types None, except for those of Forward Container. Notation
X
A type that is a model of Sequence a, b Object of type X T The value type of X t Object of type T p, q Object of type X::iterator n Object of a type convertible to X::size_type
Definitions If a is a Sequence, then p is a valid iterator in a if it is a valid (nonsingular) iterator that is reachable from a.begin(). If a is a Sequence, then [p, q) is a valid range in a if p and q are valid iterators in a and if q is reachable from p. Valid expressions In addition to the expressions defined in Forward Container, the following expressions must be valid.
Name Fill constructor Fill constructor Default fill constructor Default fill Expression
X(n, t) X a(n, t); X(n) X a(n); T T
Type requirements
X
Return type
is DefaultConstructible. is DefaultConstructible.
and j are Input Iterators whose value type is convertible to T [1] i and j are Input Iterators whose value type is convertible to T [1]
otherwise.
is mutable
void void
and j are Input Iterators whose value type is convertible to T [1]. a is mutable a.erase(p) a is mutable a.erase(p,q) a is mutable
a.clear()
is mutable
Expression semantics Semantics of an expression is defined only where it is not defined in Forward Container, or where it differs.
Name Fill construct or Fill construct or Default fill construct or Default fill construct Expression
X(n, t)
Precondition
n >= 0
X(n)
n >= 0
Postcondition size() == n. Every element is a copy of t. Creates a sequence a.size() == n. with n copies of t Every element of a is a copy of t. Creates a sequence size() == n. Every of n elements initialized to a element is a copy default value. of T(). Creates a sequence a.size() == n. with n elements initialized to Every element of a is a default value. a copy of T().
X a(n);
n >= 0
Equivalent to X(0).
size() == 0.
Front Insert
a.front()
!a.empty()
a.insert(p p , t)
Range insert
plu s the distance from i to jdoes not exceeda.max_size (). is a dereferenceable iterator in a.
Erase
a.erase(p) p
Creates a sequence that is a size() is equal to the copy of the range [i,j) distance from i to j. Each element is a copy of the corresponding element in the range [i,j). Creates a sequence that is a a.size() is equal to the distance copy of the range [i,j) from i to j. Each element in a is a copy of the corresponding element in the range [i,j). Equivalent to *(a.first()) A copy of t is inserted a.size() is incremented by before p. [2] [3] 1. *(a.insert(p,t) ) is a copy of t. The relative order of elements already in the sequence is unchanged. n copies of t are inserted a.size() is incremented by n. before p. [2] [3] [4] The relative order of elements already in the sequence is unchanged. Inserts a copy of the a.size() is range [i,j) before p. [1] [2] incremented by the [3] distance from i to j. The relative order of elements already in the sequence is unchanged. Destroys the element a.size() is pointed to by p and removes decremented by 1. The relative order of it from a. [3] the other elements in
Range erase
a.erase(p, [p,q) q)
Clear
a.clear()
the sequence is unchanged. The return value is an iterator to the element immediately following the one that was erased. Destroys the elements in the a.size() is range [p,q) and removes decremented by the them from a. [3] distance from p to q. The relative order of the other elements in the sequence is unchanged. The return value is an iterator to the element immediately following the ones that were erased. Equivalent to a.erase(a.begin(),
a.end()) a.size() == n Modifies the container so that it has exactly n elements, inserting elements at the end or erasing elements from the end if necessary. If any elements are inserted, they are copies of t. If n > a.size(), this expression is equivalent to a.insert(a.end(), n size(), t). If n < a.size(), it is equivalent to a.erase(a.begin() + n, a.end()). Equivalent to a.resize(n, a.size() == n T()).
Resize
Resize
Complexity guarantees The fill constructor, default fill constructor, and range constructor are linear. Front is amortized constant time.
Fill insert, range insert, and range erase are linear. The complexities of single-element insert and erase are sequence dependent. Invariants Models
Notes [1] At present (early 1998), not all compilers support "member templates". If your compiler supports member templates then i and j may be of any type that conforms to the Input Iterator requirements. If your compiler does not yet support member templates, however, then i and j must be of type const T* or of type X::const_iterator. [2] Note that p equal to a.begin() means to insert something at the beginning of a (that is, before any elements already in a), and p equal to a.end() means to append something to the end of a. [3] Warning: there is no guarantee that a valid iterator on a is still valid after an insertion or an erasure. In some cases iterators do remain valid, and in other cases they do not. The details are different for each sequence class. [4] a.insert(p, n, t) is guaranteed to be no slower then calling a.insert(p, t) n times. In some cases it is significantly faster. [5] Vector is usually preferable to deque and list. Deque is useful in the case of frequent insertions at both the beginning and end of the sequence, and list and slist are useful in the case of frequent insertions in the middle of the sequence. In almost all other situations, vector is more efficient. See also Container, Forward Container, Associative Container, Front Insertion Sequence, Back Insertion Sequence, vector, deque, list, slist