Common Functions |
Although each kind of STL
collection has a different set of behaviors, they all support ten common
functions. This section describes each of these functions using a vector
of t objects as a concrete
example. The first group of functions construct, destroy, and perform
size-related functions.
vector()vector
.vector(
const vector< T, Allocator >& vec
)vector
using a copy of vec .~vector()vector
and all of its entries.bool
empty() consttrue
if the vector is empty.size_type
max_size() constvector can contain.size_type
size() constvector . The following example illustrates
each of these common functions using a vector of
integers. This example makes use of a function called push_back()
, which appends a copy of its argument to the end of
the vector .
#include <iostream>
#include <vector>
void
main()
{
vector< int > v1; // Empty vector of integers.
cout << "empty = " << v1.empty() << "\n";
cout << "size = " << v1.size() << "\n";
cout << "max_size = " << v1.max_size() << "\n";
v1.push_back( 42 ); // Add an integer to the vector.
cout << "size = " << v1.size() << "\n";
cout << "v1[ 0 ] = " << v1[ 0 ] << "\n";
}
empty = 1
size = 0
max_size = 1073741823
size = 1
v1[ 0 ] = 42
The next two common functions assign one collection to another and swap the contents of two collections.
vector<
T, Allocator >& operator=( const vector< T, Allocator >& vec
)vector with a copy of vec
.void
swap( vector< T, Allocator >& vec )vector with vec . Following is an example
illustrating these two common functions using a vector
of double .
#include <iostream>
#include <vector>
void print( double_vector& vec )
{
for ( int i = 0; i < vec.size(); ++i )
cout << vec[ i ] << " ";
cout << "\n";
}
void main()
{
vector< double > v1; // Empty vector of doubles.
v1.push_back( 32.1 );
v1.push_back( 40.5 );
vector< double > v2; // Another empty vector of doubles.
v2.push_back( 3.56 );
cout << "v1 = ";
print( v1 );
cout << "v2 = ";
print( v2 );
v1.swap( v2 ); // Swap the vector's contents.
cout << "v1 = ";
print( v1 );
cout << "v2 = ";
print( v2 );
v2 = v1; // Assign one vector to another.
cout << "v2 = ";
print( v2 );
}
v1 = 32.1 40.5
v2 = 3.56
v1 = 3.56
v2 = 32.1 40.5
v2 = 3.56
There are also
non-member functions that compare and exchange vector
s.
bool
operator==( const vector< T, Allocator >& x,
const vector< T, Allocator >& y
)true
if the vector x
contains the same items in the same order as y .bool
operator<( const vector< T, Allocator >& x
, const vector< T, Allocator >& y
)true
if the vector x is
lexicographically less than y .void
swap( vector< T, Allocator >& x ,
vector< T, Allocator >& y )vector x with y
. Because the comparison operators !=
, > , < =, and
>= can be defined in terms of ==
and < , STL provides
template functions that define the last four operators in terms of the first
two. Therefore, as long as you provide operator==
and operator< for an object, the other four
are provided automatically. This is true for all objects created, not just
containers.
Following is an example using these operators to compare one vector of characters with another.
#include <iostream>
#include <vector>
void main()
{
vector< char > v1; // Empty vector of characters.
v1.push_back( 'h' );
v1.push_back( 'i' );
cout << "v1 = " << v1[ 0 ] << v1[ 1 ] << "\n";
vector< char > v2( v1 );
v2[ 1 ] = 'o'; // Replace second character.
cout << "v2 = " << v2[ 0 ] << v2[ 1 ] << "\n";
cout << "( v1 == v2 ) = " << ( v1 == v2 ) << "\n";
cout << "( v1 < v2 ) = " << ( v1 < v2 ) << "\n";
}
v1 = hi
v2 = ho
(v1 == v2) = 0
(v1 < v2) = 1
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.