Method prototype/operator | Explanation | Example |
---|---|---|
vector<T>() | default constructor; creates an empty vector to hold elements of type T | vector<int> vec1; |
vector<T>(const vector&v) | copy constructor; creates a copy of v | vector<char> vec2(vec1); |
vector<T>(int n) | creates a vector with n elements | vector<float> vec1(10); |
vector<T>(int n, const T& t) | creates a vector with n elements each with a value t | vector<char> vec1(10,'f'); |
= | assign a vector to this vector | vec1 = vec2 |
int size() | returns the number of elements in this vector | i = vec1.size() |
void resize(int n) | changes the number of elements to n. If n is larger than the current size, the vector's size is enlarged. If smaller, the vector is truncated. | vec1.resize(15) |
void resize(int 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) |
int capacity() | returns the current capacity for this vector | i = vec1.capacity() |
void reserve(int n) | changes the capacity to n if n is larger than the current capacity. Otherwise the capacity is unchanged. | vec1.reserve(20) |
T& at(int 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 vector | i = vec1.front() |
T& back() | returns the last element of the vector | i = 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() |