C++ Vectors<?XML:NAMESPACE PREFIX = O />
Constructors
Syntax:
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:
· Nothing - which creates an empty vector,
· num and val - puts num copies of val in the vector,
· from - creates a vector with the same contents as from, or
· start and end - which creates a vector containing elements from start to end.
For example, the following example constructs a vector consisting of five copies of the integer 42.
vector<int> v1( 5, 42 );
at
Syntax:
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.
erase
Syntax:
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
J
pop_back
Syntax:
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
A
Related topics: erase().
push_back
Syntax:
void push_back( const TYPE &val );
|
The push_back() function appends val to the end of the current vector.
insert
Syntax:
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:
· inserts val before loc, returning an iterator to that element,
· inserts num copies of val before loc, or
· inserts the elements from start to end before loc.
For example:
// 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
Syntax:
size_type size();
|
The size() function returns the number of elements in the current vector.