STL是C++的精华!
我们首先要学会使用:
1.Vector的使用
#include"stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
usingnamespacestd;
int_tmain(intargc,_TCHAR*argv[])
{
vector<int>iVec;
iVec.push_back(5); //进行数据的添加
iVec.push_back(1);
iVec.push_back(7);
iVec.push_back(9);
iVec.push_back(3);
iVec.push_back(2);
for(vector<int>::iteratorit= iVec.begin();it!=iVec.end();++it)//使用迭代器
cout<<*it<<" ";
cout<<endl;
sort(iVec.begin(),iVec.end()); //简单使用一下stl中的排序算法
for(vector<int>::iteratorit= iVec.begin();it!=iVec.end();++it)
cout<<*it<<" ";
cout<<endl;
intlen= iVec.size();
for(inti=0;i<len;i++) //使用下标访问操作
cout<<iVec[i]<<" ";
cout<<endl;
iVec.insert(iVec.begin()+2,4); //在第三个位置插入
vector<int>iVec1;
iVec1.push_back(2);
iVec1.push_back(2);
iVec1.push_back(22);
iVec1.push_back(22);
iVec.insert(iVec.end(),iVec1.begin(),iVec1.end()); //将iVec1插入到iVec的后面
len= iVec.size();
for(inti=0;i<len;i++) //使用下标访问操作
cout<<iVec.at(i)<<" "; //使用at的显示方式等同于[]
cout<<endl;
/*
stl vactor的三种插入方式
insert(const_iterator pos, const T& val); //在pos位置插入val数值
insert(const_iterator pos, size_type n, const T& val)//在pos处连续的插入n个val数值
insert(const_iterator pos, InputIterator first, InputIterator last)//在first和last之间的数据插入到pos位置
*/
intcapacitySize= iVec.capacity();
cout<<"Thesize is: "<<len<<endl;
cout<<"Thecapacity is: "<<capacitySize<<endl;
getchar();
return0;
}
接下来,我们实现一个简单vector(模仿一下STL)
#include"stdafx.h"
#include<iostream>
usingnamespacestd;
template<classT>
classvector
{
public:
typedefT*m_iterator;
typedefconstT*m_citerator;
typedefT&m_reference;
typedefconstT&m_creference;
staticconstsize_tmaxCapacity= 30; //首先定义最大的数据量
private:
m_iteratorm_begin; //首指针
m_iteratorm_end; //尾指针
size_tm_capacity; //容量
public:
vector()
{
m_capacity= maxCapacity;
m_begin= newT[m_capacity]; //分配的内存是容量的大小而不是size的大小
m_end= m_begin;
}
vector(size_tsize)
{
m_capacity= size>maxCapacity?maxCapacity:size; //获得正确的数量
m_start= newT[m_capacity];
m_end= m_begin;
}
~vector()
{
if(m_begin)
{
deletem_begin;
m_begin= NULL;
m_end= NULL;
m_capacity= 0;
}
}
m_citeratorgetBegin()const
{
returnm_begin;
}
m_iteratorgetBegin()
{
returnm_begin;
}
m_citeratorgetEnd()const
{
returnm_end;
}
m_iteratorgetEnd()
{
returnm_end;
}
size_tgetSize()const
{
return (m_begin==0? 0:(m_end- m_begin));
}
size_tgetCapacity()const
{
return(m_capacity);
}
boolisEmpty()const
{
returnm_begin== m_end;
}
m_citeratoroperator[](size_tpos)const
{
if(pos> m_capacity)
throwout_of_range("查询过界!");
returnm_begin[pos];
}
voidpush_back(constT& val)
{
insert(getEnd(),val);
}
voidpop_back()
{
if(m_end> m_begin)
--m_end;
}
voidinsert(m_iteratorpos,constT&val) //为了简单示例只写一个insert
{
if((size_t)(pos-m_begin+1)>= m_capacity)
reverse(2*m_capacity); //按照STL源码 也是分配倍的大小
*m_end++= val;
}
voidreverse(size_tpSize)
{
if(pSize> m_capacity)
{
m_iteratorp= newT[pSize];
intlen= getSize();
for(inti=0;i<len;i++)
*p++= *m_begin++;
delete[]m_begin;
m_begin= p-(m_end-m_begin);
m_end= p;
m_capacity=pSize;
}
}
voidclear()
{
m_begin= m_end;
}
voidswap(m_iteratorx,m_iteratory)
{
Ttemp= *x;
*x= *y;
*y= temp;
}
voidswap(T&x,T&y)
{
Ttemp= y;
y= x;
x= temp;
}
voidsort()
{
intlen= getSize();
for(inti=0;i<len;i++)
for(intj=i;j<len;j++)
if(m_begin[i]> m_begin[j])
swap(m_begin[i],m_begin[j]);
}
voidearse(m_iteratorit)
{
if(it>= m_begin&& it< m_end);
throwout_of_range("删除越界!");
while(it!=m_end)
{
*it++= *(it+1);
}
--m_end;
}
};
int_tmain(intargc,_TCHAR*argv[])
{
vector<int>iVec;
iVec.push_back(2);
iVec.push_back(1);
iVec.push_back(6);
for(vector<int>::m_iteratorit= iVec.getBegin();it!= iVec.getEnd();it++)
{
cout<<*it<<" ";
}
cout<<endl;
intlen= iVec.getSize();
cout<<"Thelength is: "<<len<<endl;
iVec.sort(); //进行简单的冒泡排序
for(vector<int>::m_iteratorit= iVec.getBegin();it!= iVec.getEnd();it++)
//可以和普通指针一样++ 没有进行运算符的重载
{
cout<<*it<<" ";
}
getchar();
return0;
}
网络上实现的一个
#include<cassert>
template<class T>
class vector
{
private:
typedef T& reference;
typedef const T& const_ref;
public:
typedef T* iterator;
typedef const T* const_it;
enum{INITCAPACITY = 20};
vector()
{
m_capacity = INITCAPACITY;
m_pb = new T[m_capacity];
m_pe = m_pb;
}
vector(size_ts)
{
m_capacity = s>INITCAPACITY ?s:INITCAPACITY;
m_pb = new T[m_capacity];
m_pe= m_pb;
}
~vector()
{
if (m_pb)
{
delete[] m_pb;
m_pb = 0;
}
m_pe = 0;
m_capacity = 0;
}
inline const_ref operator[] (size_tt) const
{
assert( t < (size_t)(m_pe- m_pb));
return m_pb[t];
}
inlineiterator begin()
{
return m_pb;
}
inline iterator end()
{
return m_pe;
}
inline const_it begin()const
{
return m_pb;
}
inline const_it end() const
{
return m_pe;
}
inline size_t size() const
{
return m_pe-m_pb;
}
inline size_t capacity()const
{
returnm_capacity;
}
inline bool empty() const
{
return m_pe == m_pb;
}
inlineconst_ref back() const
{
return *(m_pe-1);
}
void push_back(T t)
{
if ((size_t)(m_pe-m_pb+1) > m_capacity )
{
reserve(m_capacity<<1);
}
*m_pe++ =t;
}
void pop_back()
{
if(m_pe >m_pb)
{
--m_pe;
}
}
void clear()
{
m_pe = m_pb;
}
///
//Desc: 倒置容器的元素
///
void reverse()
{
iterator b = m_pb;
iterator e = m_pe;
for ( ; --e > b ; ++b )
{
ite_swap(e,b);
}
}
///
//Desc: 倒置be区间的元素
///
static void reverse( iterator b, iterator e )
{
for ( ; --e > b ; ++b )
{
ite_swap(e,b);
}
}
///
//Desc: 重新分配容器的容量
///
void reserve(size_t s)
{
if (s > m_capacity)
{
iterator p = new T[s];
for (iterator it = m_pb; it!= m_pe;)
{
*p++ = *it++;
}
delete[]m_pb;
//*****这里需要保证m_pb释放后仍然指向原来的地址*****//
m_pb = p-(m_pe-m_pb);
m_pe = p;
m_capacity =s;
}
}
///
//Desc: 删除容器内be区间的所有元素,
///
void erase(iterator b, iterator e )
{
assert(b>= m_pb && b < m_pe);
assert(e > m_pb&& e <= m_pe);
while( e != m_pe)
{
*b++ = *e++;
}
m_pe -=(e-b);
}
///
//Desc: 删除容器内的一个元素,
///
void erase(iterator it)
{
assert(it >=m_pb && it < m_pe);
while( it !=m_pe )
{
*it++ = *(it+1);
}
--m_pe;
}
///
//Desc: 将be区间的元素复制到容器尾部
///
void copyback(const_it b, const_it e )
{
assert( b < e );
for( ; b != e; ++b )
{
if ( (size_t)(m_pe-m_pb+1) > m_capacity )
{
reserve(m_capacity<<1);
}
*m_pe++ = *b;
}
}
///
//Desc: 将be区间符合条件的元素复制到容器尾部
///
void copyback(const_it b, const_it e, bool(*f)(const_ref))
{
assert( b < e );
for ( ; b != e; ++b)
{
if ( f(*b))
{
if ((size_t)(m_pe-m_pb+1) > m_capacity )
{
reserve(m_capacity<<1);
}
*m_pe++ = *b;
}
}
}
iterator find_if( bool(*f)(const_ref) )
{
iterator it;
for ( it = m_pb; it !=m_pe; ++it)
{
if ( f(*it) )
{
break;
}
}
return it;
}
///
//Desc: 在容器内查找第一个元素,如果找到返回指针,否则返回超尾
///
iterator find( const_ref t, bool(*f)(const_ref, const_ref) )
{
iterator it;
for ( it = m_pb; it !=m_pe; ++it)
{
if ( f(*it,t) )
{
break;
}
}
return it;
}
///
//Desc: 在be区间查找第一个元素,如果找到返回指针,否则返回超尾
///
static iterator find_if( iterator b, iterator e,bool(*f)(const_ref) )
{
assert( b < e );
iterator it;
for ( it = b; it != e; ++it)
{
if ( f(*it) )
{
break;
}
}
return it;
}
///
//Desc: 查找与最后一个元素,如果找到返回指针,否则返回超尾
///
iterator find_end( const_ref val, bool(*f)(const_ref, const_ref))
{
for (iterator it = m_pe-1; it != m_pb-1; --it)
{
if ( f(*it , val) )
{
return it;
}
}
return m_pe;
}
iterator find_end( bool(*f)(const_ref) )
{
for (iterator it = m_pe-1;it != m_pb-1; --it)
{
if ( f(*it) )
{
return it;
}
}
return m_pe;
}
///
//Desc: 查找符合条件的元素数量
///
size_t count_if( bool(*f)(const_ref) )
{
size_t count =0;
for (iterator it = m_pb; it != m_pe;++it)
{
if ( f(*it))
{
++count;
}
}
return count;
}
///
//Desc: 查找符合条件的元素数量,可以比较相等或者不相等
///
size_t count_if( const_ref val, bool(*f)(const_ref, const_ref) )
{
size_t count =0;
for (iterator it =m_pb; it != m_pe; ++it)
{
if ( f(*it,val))
{
++count;
}
}
return count;
}
///
//Desc: 在容器内查找元素,将其删除,并移动超尾
///
void remove_if(bool(*f)(const_ref) )
{
for(iterator it = m_pb; it != m_pe; )
{
if (f(*it))
{
for (iterator it1 = it;it1!= m_pe; ++it1)
{
*it1=*(it1+1) ;
}
--m_pe;
}
else
{
++it;
}
}
}
static voidite_swap( iterator x, iterator y )
{
T t = *x;
*x = *y, *y = t;
}
///
//Desc: 依照条件排序b'e区间的元素
///
static void sort(iterator b, iterator e,bool(*f)(const_ref,const_ref))
{
assert( b <= e);
for ( ; b != e; ++b )
{
for (iterator it = b+1; it!= e; ++it)
{
if ( f(*it,*b) )
ite_swap(it, b);
}
}
}
///
//Desc: 依照条件排序b'e区间的元素
///
void sort( bool(*f)(const_ref,const_ref) )
{
iterator b = m_pb;
for ( ; b != m_pe; ++b )
{
for (iterator it = b+1; it!= m_pe; ++it)
{
if ( f(*it,*b) )
ite_swap(it, b);
}
}
}
template< class F >
static void for_each(iterator b,iterator e, F(*f)(reference))
{
assert( b <= e);
while( b != e)
{
f(*b++);
}
}
template< class F >
void for_each( F(*f)(reference) )
{
iterator b =m_pb;
while( b != m_pe)
{
f(*b++);
}
}
///
//Desc: 随机打乱b'e区间的元素
///
static void random_shuffle(iterator b, iterator e)
{
assert( b < e);
iterator it = b;
for (size_t u = 1; ++it != e; ++u)
{
size_t m = 0x7FFF;
size_t n = rand() & 0x7FFF;
for (; m < u && m != ~0L; m = (m<<0xF) |0x7FFF )
{
n = (n<<0xF) |0x7FFF;
}
ite_swap( it, b+(n%u) );
}
}
private:
iterator m_pb;
iterator m_pe;
size_t m_capacity;
};