0% found this document useful (0 votes)
33 views5 pages

Container: Description

A Sequence is a variable-sized container whose elements are arranged in a strict linear order and supports insertion and removal of elements. It has constructors to initialize with a size and value, fill with a default value, or copy a range. It supports accessing the front element, inserting single elements or ranges at positions or filling, erasing single elements or ranges, clearing, and resizing. Operations have linear or amortized constant time complexity depending on the sequence type, with vector usually being most efficient.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Container: Description

A Sequence is a variable-sized container whose elements are arranged in a strict linear order and supports insertion and removal of elements. It has constructors to initialize with a size and value, fill with a default value, or copy a range. It supports accessing the front element, inserting single elements or ranges at positions or filling, erasing single elements or ranges, clearing, and resizing. Operations have linear or amortized constant time complexity depending on the sequence type, with vector usually being most efficient.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Description A Sequence is a variable-sized Container whose elements are arranged in a strict linear order.

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.

constructor X(i, j) Range constructor


X a(i, j); Range constructor

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]

Front Insert Fill insert Range insert

a.front() a.insert(p, t) a.insert(p, n, t) a.insert(p, i, j)

reference if a is mutable, const_reference X::iterator a i

otherwise.

is mutable

void void

Erase Range erase Clear Resize Resize

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()

iterator iterator void void void

is mutable a.resize(n, a is mutable


t) a.resize(n) a

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

Semantics Creates a sequence with n copies of t

X a(n, t); 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

or Default X a; or X() construct or X(i, j) Range [i,j) is a valid construct range. or

Equivalent to X(0).

size() == 0.

X a(i, j); [i,j) is a valid Range construct range. or

Front Insert

a.front()

!a.empty()

a.insert(p p , t)

is a valid iterator in a.a.size() <


a.max_size()

Fill insert a.insert(p p is a valid iterator , n, t) in a.n >= 0 &&


a.size() + n <= a.max_size().

Range insert

a.insert(p [i,j) is a valid , i, j) range.a.size()

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)

is a valid range ina.

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

a.resize(n n <= , t) a.max_size()

Resize

a.resize(n n <= ) a.max_size()

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

vector [5] deque list slist

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

You might also like