C++ Reference Material
STL Sequential Container Classes
Member Function Summary
This page contains two tables. The first lists all member functions common to all three types of sequential container (vector, deque, list). The second table lists all member functions that appear in one or two of these three containers, but not in all three.
constructors |
---|
ContainerType<T> c;
Constructs an empty container which can hold values of component type T. |
ContainerType<T> c(num);
Constructs a container of size num, with each component initialized to the default value of component type T(). |
ContainerType<T> c(num, val);
Constructs a container of size num with each component initialized to the value val, which must be of type T. |
ContainerType<T> c(inIterBegin, inIterEnd);
Constructs a container with values from the range [inIterBegin,inIterEnd) in another container (which does not necessarily have to be of the same container type as c, as long as its component type is T). |
ContainerType<T> c(otherLikeContainer);
ContainerType<T> c = otherLikeContainer; Copy constructor, showing both syntactic forms. |
assignment operator |
c1 = c2
Assigns one container to another like container (vector to vector, deque to deque, list to list), and return the assigned container. |
comparison operators |
c1 == c2
Returns true if c1 and c2 are two like containers which have the same size and contain the same values in the same order, and otherwise return false. |
c1 != c2
Returns true if c1 == c2 returns false, and otherwise return false. |
c1 < c2
Returns true if c1 and c2 are two like containers and if, in the pairwise comparison of the values from c1 and c2, in the first pair in which the two differ the component from c1 is less than the component from c2; otherwise return false. |
c1 <= c2
Returns true if either c1 < c2 or c1 == c2 is true, and otherwise return false. |
c1 > c2
Returns true if c2 < c1 is true, and otherwise return false. |
c1 >= c2
Returns true if either c1 > c2 or c1 == c2 is true, and otherwise return false. |
testing for empty |
empty()
Returns true if container is empty, and otherwise return false. |
getting/setting container size |
size()
Returns number of components currently in container. |
max_size()
Returns maximum number of components a container of the given type can hold. |
resize(num, val)
Changes the size of the container to the value specified by num. If the container must be lengthened (and only in this case), components with value val are added to the end. If the container is shortened, values are lost. |
getting iterator/reference access to container locations |
begin()
Returns an iterator (or const_iterator) pointing to the first component of a container. |
end()
Returns an iterator (or const_iterator) pointing to one-past-the-last component of a container. |
rbegin()
Returns a reverse_iterator (or const_reverse_iterator) pointing to the last component of a container. |
rend()
Returns a reverse_iterator (or const_reverse_iterator) pointing to one-before-the-first component of a container. |
front()
Returns a reference (or const_refernce) to the first component in the container. |
back()
Returns a reference (or const_refernce) to the last component in the container. |
inserting one or more values |
push_back(val)
Adds a component with value val to the end of the container. |
insert(iter, val)
Inserts a component with value val immediately before the component pointed to by iter and return an iterator pointing to the inserted component. |
insert(iter, num, val)
Inserts num copies of val immediately before the component pointed to by iter. |
insert(iter, inIterBegin, inIterEnd)
Inserts, immediately before the component pointed to by iter, values from the range [inIterBegin,inIterEnd) in another (and possibly different kind of) container, but whose component type matches that of the receiving container. |
assigning values |
assign(inIterBegin, inIterEnd)
Assigns to the container the sequence of values from the range [inIterBegin,inIterEnd), overwriting all current components. |
assign(num, val)
Assigns to the container num components having the value val, overwriting all current components. |
deleting one or more values |
pop_back()
Removes the last component in the container. |
erase(iter)
Removes the component pointed to by iter and return an iterator pointing to the component after the one removed (or to one-past-the-last if the last component was the one removed). |
erase(iterBegin, iterEnd)
Removes those components in the range [iterBegin,iterEnd) and return an iterator pointing to the component after the last one removed (or to one-past-the-last, if the range removed included the last component). |
clear()
Removes all components from the container, and reduce its size to zero. |
miscellaneous operations |
swap(otherLikeContainer)
Exchanges components in the invoking container with those in otherLikeContainer (vector with vector, deque with deque, list with list). |
get_allocator()
Returns the allocator of the container. |
vector-only member functions |
---|
reserve(num)
Sets the capacity of a vector so that it is equal to at least num. |
capacity()
Returns the current capacity of a vector. |
list-only member functions |
merge(otherList)
Merges the list contained in otherList with the invoking list and clear otherList. (Both lists are assumed to be ordered and the result is also ordered.) |
merge(otherList, binPred)
Same as preceding merge(), except that in this version a comparitor (binary predicate) binPred is provided to determine when one component precedes another. |
remove(val)
Removes all components with value val from a list. |
remove_if(unPred)
Removes all components for which the unary predicate function unPred returns true. |
reverse()
Reverse the order of all components of a list. |
sort()
Sorts the components of a list into ascending order. |
sort(binPred)
Sorts the components of a list using the comparitor binPred to determine when one component precedes another. |
splice(iter, otherList)
Removes all components from otherList and insert them just before location iter. |
splice(iter, otherList, otherIter)
Removes component pointed to by otherIter from otherList and insert it just before location iter. |
splice(iter, otherList, otherIterBegin, otherIterEnd)
Removes components from range [otherIterBegin,otherIterEnd) in otherList and insert them just before location iter. |
unique()
Removes consecutive duplicate items. |
unique(binPred)
Same as preceding unique() but uses binPred to determine when two components are duplicates. |
vector-and-deque-only member functions |
at(index)
Returns a reference (or const_reference) to the component specified by index. (Throws an out_of_range exception if index is out of bounds.) |
operator[](index)
Returns a reference (or const_reference) to the component specified by index. (Does not throw an out_of_range exception if index is out of bounds.) |
deque-and-list-only member functions |
push_front(val)
Adds an component with value val to the front of the container. |
pop_front()
Removes the first component from the container. |