Vector是什么?
vector是C++(STL)中的一种序列容器 Vector是一个动态数组,内存空间是连续的,支持随机访问,支持迭代器访问
Vector代码实现
变量指向
代码初始化
# include <iostream>
using namespace std;
# include <assert.h>
# include <vector>
namespace xiaofeng
{
template < class T >
class vector
{
public :
typedef T* iterator;
typedef const T* const_iterator;
vector ( )
: _start ( nullptr )
, _finish ( nullptr )
, _end_of_storage ( nullptr )
{ }
private :
iterator _start;
iterator _finish;
iterator _end_of_storage;
} ;
起始与结束位置
iterator begin ( )
{
return _start;
}
iterator end ( )
{
return _finish;
}
const_iterator begin ( ) const
{
return _start;
}
const_iterator end ( ) const
{
return _finish;
}
resize
void resize ( size_t n, T val = T ( ) )
{
if ( n < size ( ) )
{
_finish = _start + n;
}
else
{
if ( n > capacity ( ) )
{
reserve ( n) ;
}
while ( _finish != _start + n)
{
* _finish = val;
++ _finish;
}
}
}
reverse
void reserve ( size_t n)
{
if ( n > capacity ( ) )
{
size_t sz = size ( ) ;
T* tmp = new T[ n] ;
if ( _start)
{
memcpy ( tmp, _start, sizeof ( T) * size ( ) ) ;
delete [ ] _start;
}
_start = tmp;
_finish = _start + sz;
_end_of_storage = _start + n;
}
}
push_back
void push_back ( const T& x)
{
if ( _finish == _end_of_storage)
{
reserve ( capacity ( ) == 0 ? 4 : capacity ( ) * 2 ) ;
}
* _finish = x;
++ _finish;
}
pop
void pop_back ( )
{
assert ( ! empty ( ) ) ;
-- _finish;
}
insert
iterator insert ( iterator pos, const T& val)
{
assert ( pos >= _start) ;
assert ( pos <= _finish) ;
if ( _finish == _end_of_storage)
{
size_t len = pos - _start;
reserve ( capacity ( ) == 0 ? 4 : capacity ( ) * 2 ) ;
pos = _start + len;
}
iterator end = _finish - 1 ;
while ( end >= pos)
{
* ( end + 1 ) = end;
-- end;
}
* pos = val;
++ _finish;
return pos;
}
erase
void erase ( iterator pos)
{
assert ( pos >= _start) ;
assert ( pos < _finish) ;
iterator start = pos + 1 ;
while ( start != _finish)
{
* ( start + 1 ) = * start;
++ start;
}
-- _finish;
}
capacity
size_t capacity ( ) const
{
return _end_of_storage - _start;
}
size
size_t size ( ) const
{
return _finish - _start;
}
empty
bool empty ( )
{
return _finish == _start;
}
operator[]
T& operator [ ] ( size_t pos)
{
assert ( pos < size ( ) ) ;
return _start[ pos] ;
}
const T& operator [ ] ( size_t pos) const
{
assert ( pos < size ( ) ) ;
return _start[ pos] ;
}
测试用例
插入与删除
vector< int > v1;
v1. push_back ( 1 ) ;
v1. push_back ( 2 ) ;
v1. push_back ( 3 ) ;
v1. push_back ( 4 ) ;
cout << "push_back()" << endl;
for ( size_t i = 0 ; i < v1. size ( ) ; i++ )
{
cout << v1[ i] << endl;
}
v1. pop_back ( ) ;
v1. pop_back ( ) ;
v1. pop_back ( ) ;
v1. pop_back ( ) ;
cout << "pop()" << endl;
for ( size_t i = 0 ; i < v1. size ( ) ; i++ )
{
cout << v1[ i] << endl;
}
扩容
vector< int > v1;
v1. push_back ( 1 ) ;
v1. push_back ( 2 ) ;
v1. push_back ( 3 ) ;
v1. push_back ( 4 ) ;
v1. push_back ( 1 ) ;
v1. push_back ( 2 ) ;
v1. push_back ( 3 ) ;
v1. push_back ( 4 ) ;
cout << "size:" << v1. size ( ) << endl;
cout << "capacity:" << v1. capacity ( ) << endl;
v1. reserve ( 100 ) ;
cout << "reserve-->size:" << v1. size ( ) << endl;
cout << "reserve-->capacity:" << v1. capacity ( ) << endl;
v1. resize ( 1 ) ;
cout << "resize-->size:" << v1. size ( ) << endl;
cout << "resize-->capacity:" << v1. capacity ( ) << endl;
迭代器失效
std:: vector< int > v1;
v1. push_back ( 1 ) ;
v1. push_back ( 2 ) ;
v1. push_back ( 3 ) ;
v1. push_back ( 4 ) ;
for ( auto e : v1)
{
cout << e << " " ;
}
cout << endl;
auto pos = find ( v1. begin ( ) , v1. end ( ) , 3 ) ;
if ( pos != v1. end ( ) )
{
pos = v1. insert ( pos, 30 ) ;
}
for ( auto e : v1)
{
cout << e << " " ;
}
cout << endl;
( * pos) ++ ;
* ( pos+ 1 ) ++ ;
for ( auto e : v1)
{
cout << e << " " ;
}
cout << endl;