引言
如果有一天!你骄傲离去!(抱歉搞错了) 如果有一天,你在简历上写下了这段话: 那么你不得不在面试前实现一下STL常见的容器了。 C++的常用容器有:vector、string、deque、stack、queue、list、set、map。接下来就让我们对每种常用容器进行介绍和实现吧。
一、vector
# include <assert.h>
# include <algorithm>
template < class T >
class vector
{
public :
typedef T* iterator;
typedef const T* const_iterator;
iterator begin ( )
{
return _start;
}
iterator end ( )
{
return _finish;
}
const_iterator cbegin ( ) const
{
return _start;
}
const_iterator cend ( ) const
{
return _finish;
}
T& operator [ ] ( size_t pos)
{
assert ( pos < size ( ) ) ;
return _start[ pos] ;
}
const T& operator [ ] ( size_t pos)
{
assert ( pos < size ( ) ) ;
return start[ pos] ;
}
vector ( ) : _start ( nullptr ) , _finish ( nullptr ) , _endOfStorage ( nullptr ) { }
template < class InputIterator >
vector ( InputIterator first, InputIterator last) : _start ( nullptr ) , _finish ( nullptr ) , _endOfStorage ( nullptr )
{
while ( first != last)
{
push_back ( * first) ;
first++ ;
}
}
vector ( const vector< T> & v) : _start ( nullptr ) , _finish ( nullptr ) , _endOfStorage ( nullptr )
{
vector< T> tmp ( v. cbegin ( ) , v. cend ( ) ) ;
swap ( tmp) ;
}
vector ( size_t n, const T& val = T ( ) )
{
reserve ( n) ;
for ( size_t i = 0 ; i < n; ++ i)
{
push_back ( val) ;
}
}
vector< T> & operator = ( vector< T> v)
{
swap ( v) ;
return * this ;
}
~ vector ( )
{
delete [ ] _start;
_start = _finish = _endOfStorage = nullptr ;
}
void reserve ( size_t n)
{
if ( n > capacity ( ) )
{
size_t oldSize = size ( ) ;
T* tmp = new T[ n] ;
if ( _start != nullptr )
{
for ( int i = 0 ; i < size ( ) ; ++ i)
{
tmp[ i] = _start[ i] ;
}
delete [ ] _start;
}
_start = tmp;
_finish = tmp + oldSize;
_endOfStorage = _start + n;
}
}
void reserve ( size_t n, T val = T ( ) )
{
if ( n > capacity ( ) )
{
reserve ( n) ;
}
if ( n > size ( ) )
{
while ( _finish < _start + n)
{
* _finish = val;
_finish++ ;
}
}
else
{
_finish = _start + n;
}
}
size_t size ( ) const
{
return _finish - _start;
}
size_t capacity ( ) const
{
return _endOfStorage - _start;
}
bool empty ( ) const
{
return _finish == _start;
}
void clear ( )
{
_finish = _start;
}
void push_back ( const T& x)
{
if ( _finish == _endOfStorage)
{
size_t newCapacity = ( capacity ( ) == 0 ? 4 : capacity ( ) * 2 ) ;
reserve ( newCapacity) ;
}
* _finish = x;
_finish++ ;
}
void pop_back ( )
{
assert ( ! empty ( ) ) ;
_finish-- ;
}
void insert ( iterator pos, const T& val)
{
assert ( pos < _finish) ;
assert ( pos >= _start) ;
if ( _finish == _endOfStorage)
{
size_t len = pos - _start;
size_t newCapacity = capacity ( ) == 0 ? 4 : capacity ( ) * 2 ;
reserve ( newCapacity) ;
pos = _start + len;
}
iterator end = _finish - 1 ;
while ( end >= pos)
{
* ( end + 1 ) = * end;
end-- ;
}
* pos = val;
_finish++ ;
}
iterator erase ( iterator pos)
{
assert ( pos >= _start) ;
assert ( pos < _finish) ;
iterator begin = pos;
while ( begin < _finish - 1 )
{
* ( begin) = * ( begin + 1 ) ;
begin++ ;
}
_finish-- ;
return pos;
}
void swap ( vector< T> & v)
{
std:: swap ( _start, v. _start) ;
std:: swap ( _finish, v. _finish) ;
std:: swap ( _endOfStorage, v. _endOfStorage) ;
}
private :
iterator _start;
iterator _finish;
iterator _endOfStorage;
} ;
二、list
三、map