cppreference.com -> C++ Vectors -> Details |
vector(); vector( size_type num, const TYPE &val ); vector( const vector &from ); vector( input_iterator start, input_iterator end ); |
C++ Vectors can be constructed with either:
vector<int> v1( 5, 42 );
v1 == v2 v1 != v2 v1 <= v2 v1 >= v2 v1 < v2 v1 > v2 v[] |
The C++ Vectors can be compared using the standard operators: ==, !=, <=, >=, <, and >. Individual elements of a vector can be examined with the [] operator.
Two vectors are equal if
void assign( input_iterator start, input_iterator end ); void assign( size_type num, const TYPE &val ); |
The assign() function either gives the current vector the values from start to end, or gives it num copies of val. This function will destroy the previous contents of the vector.
TYPE at( size_type loc ); |
The at() function returns a reference to the element in the current vector at loc. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector. For example, consider the following code:
vector<int> v( 5, 1 ); for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v[i] << endl; }
This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:
vector<int> v( 5, 1 ); for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v.at(i) << endl; }
Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.
Related topics:
TYPE back(); |
The back() function returns a reference to the last element in the current vector. For example:
vector<int> v; for( int i = 0; i < 5; i++ ) { v.push_back(i); } cout << "The first element is " << v.front() << " and the last element is " << v.back() << endl;
This code produces the following output:
The first element is 0 and the last element is 4Related topics:
iterator begin(); |
The function begin() returns an iterator to the first element in the current vector. For example, the following code uses an iterator to display all of the elements in a vector:
vector<int> v1( 5, 789 ); vector<int>::iterator it; for( it = v1.begin(); it != v1.end(); it++ ) cout << *it << endl;Related topics:
size_type capacity(); |
The capacity() function returns the number of elements that the current vector can hold before it will need to allocate more space.
void clear(); |
The function clear() deletes all of the elements in the current vector.
bool empty(); |
The empty() function returns true if the current vector has no elements, false otherwise. For example, the following code clears a given vector and displays its contents in reverse order:
vector<int> v; for( int i = 0; i < 5; i++ ) { v.push_back(i); } while( !v.empty() ) { cout << v.back() << endl; v.pop_back(); }Related topics:
iterator end(); |
The end() function returns an iterator just past the end of the current vector. Note that before you can access the last element of the vector, you'll have to decrement the iterator.
Related topics:
iterator erase( iterator loc ); iterator erase( iterator start, iterator end ); |
The erase() function either deletes the element at location loc, or deletes the elements between start and end. The return value is the element after the last element erased. For example:
// Create a vector, load it with the first ten characters of the alphabet vector<char> alphaVector; for( int i=0; i < 10; i++ ) alphaVector.push_back( i + 65 ); int size = alphaVector.size(); vector<char>::iterator startIterator; vector<char>::iterator tempIterator; for( int i=0; i < size; i++ ) { startIterator = alphaVector.begin(); alphaVector.erase( startIterator ); // Display the vector for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) cout << *tempIterator; cout << endl; }
That code would display the following output:
BCDEFGHIJ CDEFGHIJ DEFGHIJ EFGHIJ FGHIJ GHIJ HIJ IJ JRelated topics:
TYPE front(); |
The front() function returns a reference to the first element of the current vector.
Related topics:
allocator_type get_allocator(); |
The function get_allocator() returns the current vector's allocator.
iterator insert( iterator loc, const TYPE &val ); void insert( iterator loc, size_type num, const TYPE &val ); void insert( iterator loc, input_iterator start, input_iterator end ); |
The function insert() either:
// Create a vector, load it with the first 10 characters of the alphabet vector<char> alphaVector; for( int i=0; i < 10; i++ ) alphaVector.push_back( i + 65 ); // Insert four C's into the vector vector<char>::iterator theIterator = alphaVector.begin(); alphaVector.insert( theIterator, 4, 'C' ); // Display the vector for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ ) cout << *theIterator;
This code would display:
CCCCABCDEFGHIJ
size_type max_size(); |
The max_size() function returns the maximum number of elements that the current vector can hold.
void pop_back(); |
The function pop_back() deletes the last element in the current vector. For example:
vector<char> alphaVector; for( int i=0; i < 10; i++ ) alphaVector.push_back( i + 65 ); int size = alphaVector.size(); vector<char>::iterator theIterator; for( int i=0; i < size; i++ ) { alphaVector.pop_back(); for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ ) cout << *theIterator; cout << endl; }
This code displays the following output:
ABCDEFGHI ABCDEFGH ABCDEFG ABCDEF ABCDE ABCD ABC AB ARelated topics:
void push_back( const TYPE &val ); |
The push_back() function appends val to the end of the current vector.
reverse_iterator rbegin(); |
The rbegin() function returns a reverse iterator to the end of the current vector.
reverse_iterator rend(); |
The function rend() returns a reverse iterator to the beginning of the current vector.
void reserve( size_type size ); |
The reserve() function sets the capacity of the current vector to at least size.
void resize( size_type size, TYPE val ); |
The function resize() changes the size of the current vector to size, setting any newly-created elements to val.
size_type size(); |
The size() function returns the number of elements in the current vector.
Related topics:
void swap( vector &from ); |
The swap() function exchanges the elements of the current vector with those of from.