关于STL vector的实现 答对加100

2025-03-22 07:40:40
推荐回答(1个)
回答1:

今天看了下源码,确实是通过判断是否是int类型进行不同操作的,具体如下:

首先是这个函数:
template
vector(_InputIterator __first, _InputIterator __last,
const allocator_type& __a = allocator_type()) : _Base(__a) {
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_initialize_aux(__first, __last, _Integral());
}
定义了一个类型:
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
这个类型会根据_InputIterator的不同而进行不一样的类模板显示具体化,比如说,如果是int类型的话,
__STL_TEMPLATE_NULL struct _Is_integer {
typedef __true_type _Integral;
};

会将__true_type定义为_Integral,其他非数值情况,会用默认的模板实例化:
template struct _Is_integer {
typedef __false_type _Integral;
};
会将__false_type定义为_Integral

然后调用:
_M_initialize_aux(__first, __last, _Integral());

_M_initialize_aux这个函数有重载,会根据_Integral()是__false_type还是__true_type调用不同的重载函数,
两个重载如下:
template
void _M_initialize_aux(_Integer __n, _Integer __value, __true_type) {
_M_start = _M_allocate(__n);
_M_end_of_storage = _M_start + __n;
_M_finish = uninitialized_fill_n(_M_start, __n, __value);
}

template
void _M_initialize_aux(_InputIterator __first, _InputIterator __last,
__false_type) {
_M_range_initialize(__first, __last, __ITERATOR_CATEGORY(__first));
}

之后的就简单了,就是根据是否是int做出不同的操作就是了

======================================================================
我看的是vc的vector文件,有点不太一样,呵呵

是我想当然了,确实是用模板类的,不过写的挺麻烦的、

你可以到网上搜一下stl的源码,然后用Source Insight这个软件跟踪一下

还是看源码最容易理解,我也得看一下了,不懂的还是很多啊
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
这样说也有点不明白,给你写了个简单点的,代码如下,我只是写个简单的例子,迭代器的实现很麻烦的:
#include
using namespace std;
class _It
{
public:
_It(){}
_It(int *a):p(a){}
int* operator ++(int)
{
int *q=p;
p++;
return q;
}
bool operator <=(_It &oth)
{
return (this->p)<=(oth.p);
}
int operator -(_It &oth)
{
return (this->p)-(oth.p);
}
private:
int *p;
};
template
class myvector
{
public:
myvector(int n,T a)
{
data=new T[n];
for(int i=0;i data[i]=a;
len=n;
}
myvector(_It s,_It e)
{
len=e-s;
data=new T[len];
T *p=data;
while(s<=e)
*p++=*s++;
}

void print()
{
for(int i=0;i cout< cout< }
private:
T *data;
int len;
};
void main()
{
myvector v1( 5, 6 );
v1.print();
int a[] = {1,2,3};
myvector v2(a, a+3);
v2.print();

}
==============================================================
int a[] = {1,2,3}; vector v(a, a+3);
这种实现不是因为模板,而是因为int *这种指针也是迭代器的一种,就是说
int *可以转化为迭代器,而不是因为模板的类型替换

比如说
class A
{
A(int){}
}
这样的类,是可以这样用的:
如果一个函数的定义是:
void func(A x){}
则调用时可以用:
func(5);
因为A中有转换的构造函数

这里也是一样的,可以用
int a[] = {1,2,3}; vector v(a, a+3);
是因为int *可以转化为迭代器,

不知道这样说明白了没有

其实我对stl的机制也有很多不明白的
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
vector里面template中的参数和迭代器不是一个吧,如下:
template >

typedef const_iterator _It;
vector(_It _F, _It _L, const _A& _Al = _A())

差不多这个意思:
template
vector(_Iter _First, _Iter _Last)

不会报错