C++ Notes: STL Vector, list, and deque operations

The sequence container template classes (vector, list, deque) have many common operations which makes it easy to learn and use them. Assume that T is some type (eg, int) and the following declarations:, where seq is one of the sequence containers (vector, list, or deque).
   T e;
seq<T> c, c1;
seq<T>::iterator iter, iter2, beg, end;
seq<T>::reverse_iterator riter; // beg, end could also be here
int i, n;
bool b;

Common sequence container constructors, functions, operators, etc

Result Method V
L
D
 
Description
Constructors and destructors
  seq<T> c; V
L
D

Creates an empty seq of T's.
  seq<T> c(n); V
-
D

Creates seq of n default values.
  seq<T> c(n, e); V
L
D

Creates seq of n copies of e.
  seq<T> c(beg, end); V
L
D

Creates seq with elements copied from range beg..end.
  c.~seq<T>(); V
L
D

Destroys all elems and frees memory.
Size
i =  c.size(); V
L
D

Number of elements.
b =  c.empty(); V
L
D

True if empty. Same as c.size()==0
Altering
c =  c1; V
L
D

Assigns c1 to c.
c[i] e; V
-
D

Sets ith element. Subscripts from zero.
c.at(i) =  e; V
-
D

As subscription, but may throw out_of_range.
c.front() =  e; V
L D

Same as c[0] = e.
c.back() =  e; V
L D

Same as c[c.size()-1] = e.
  c.push_back(e); V
L
D

Adds e to end of c. Expands c if necessary.
  c.pop_back(); V
L
D

Removes last element of c.
  c.push_front(e); -
L
D

Adds e to front of c. Expands c if necessary.
  c.pop_front(); -
L
D

Removes first element of c.
  c.clear(); V
L
D

Removes all elements.
iter =  c.assign(n, e); V
L
D

Replaces existing elements with n copies of e.
iter =  c.assign(beg, end); V
L
D

Replaces existing elems with copies from range beg..end.
iter2 =  c.insert(iter, e); V
L
D

Inserts a copy of e at iter position and returns its position.
  c.insert(iter, n, e); V
L
D

Inserts n copies of e starting at iter position.
  c.insert(iter, beg, end); V
L
D

Inserts all elements in range beg..end, starting at iter position.
iter2 =  c.erase(iter); V
L
D

Removes elem at iter and returns position of next elem.
iter =  c.erase(beg, end); V
L
D

Removes range beg..end and returns position of next element.
Access
e =  c.front(); V
L
D

First element. No range checking.
e =  c.back(); V
L
D

Last element. No range checking.
e =  c[i]; V
-
D

ith element. No range checking.
e =  c.at(i); V
-
D

As subscription, but may throw out_of_range.
Iterators (operators apply to both forward and reverse iterators)
e =  *iter; V
L
D

Dereference the iterator to get the value.
iter =  c.begin(); V
L
D

Returns iterator to first element.
iter =  c.end(); V
L
D

Returns iterator to after last element.
riter =  c.rbegin(); V
L
D

Returns iterator to first (in reverse order) element.
riter =  c.rend(); V
L
D

Returns iterator to after last (in reverse order) element.

++iter; V
L
D

Preincrement iter to next elem. Prefer to postincrement.

--iter; V
L
D

Predecrement iter. Can also postincrement. Returns value.
iter2 =  iter + i; V
-
D

Iterator i elements after iter. += assigment also defined.
iter2 =  iter - i; V
-
D

Iterator i elements before iter -= assignment also defined.
Value-based functions
...






Algorithms - implemented as member function. (a indicates non-member in <algorithm>)
...