Some vector class methods and operators

Method prototype/operatorExplanationExample
vector<T>()default constructor; creates an empty vector to hold elements of type Tvector<int> vec1;
vector<T>(const vector&v)copy constructor; creates a copy of vvector<char> vec2(vec1);
vector<T>(size_t n)creates a vector with n elementsvector<float> vec1(10);
vector<T>(size_t n, 
          const T& t)
creates a vector with n elements each with a value tvector<char> vec1(10,'f');
=assign a vector to this vectorvec1 = vec2
void assign(size_t n,
            const T& t)
assigns a vector of length n with all entries equal to t to this vector. The size will be n.vec1.assign(15,5)
void resize(size_t n)changes the number of elements to n. If n is larger than the current size, the vector's size is enlarged and all new elements set to the default for T. If smaller, the vector is truncated. vec1.resize(15)
void resize(size_t n, 
            const T& t)
changes the number of elements to n. If n is larger than the current size, the vector's size is enlarged and all new elements at set to t. If smaller, the vector is truncated. vec1.resize(15,5)
size_t capacity()returns the current capacity for this vectori = vec1.capacity()
size_t size()returns the actual number of elements in this vectori = vec1.size()
void reserve(size_t n)changes the capacity to n if n is larger than the current capacity. Otherwise the capacity is unchanged.vec1.reserve(20)
T& at(size_t pos)returns the element at position pos. Remember that vectors start at position 0! This is an l-value, that is, it can be used on the left hand side of an assignment statement.c = vec1.at(5)
vec1.at(5) = 't'
[ ]same as at except that it may not be checked.c = vec1[5])
vec1[5] = 't'
bool empty()returns true if size() == 0.if (vec1.empty()) ...
T& front()returns the first element of the vectori = vec1.front()
T& back()returns the last element of the vectori = vec1.back()
void push_back(const T&t)appends t to the back of this vector. The size is incremented.vec1.push_back(3.4)
void pop_back()removes the last element from this vector. The size is decremented.vec1.pop_back()
void clear()removes all elements from this vector. The size is 0.vec1.clear()