注册 登录
编程论坛 C++教室

大家一起来写STL

shower 发布于 2007-02-15 19:45, 1123 次点击
大家好,我是一名大二的学生。我学的是软件工程。我们上学期学的是数据结构,下学期学算法。我想多多练习总是没有错的,于是有自己连编写STL的冲动。STL是一个很好的数据结构与算法的集合。我手上有《STL源码剖析》(by候健),我一边学一边编。很希望和大家一起交流,如果有兴趣,请发邮件到shao_wen_buaa@163.com.
另外我还有个问题:
我在写vector的时候遇到这样的问题:
template<typename T>
void SW_Vector<T>::push_back(const T& x)//insert x at the end of the vector
{
if (!full())
{
data[finish++]=x;
}
else
{
int i;
T* new_data;
T* old_data;
old_data=data;
new_data=new T(2*size());
//copy the old values
for (i=start;i<finish;i++)
{
new_data[i]=data[i];
}
new_data[i]=x;

data=new_data;
finish++;
end_of_storage*=2;

//start remain the same
//delete
delete []old_data; //*********************
}
}
这个函数把x插入到vector后面,但是到*******的句子总是出内存错误,我把它改成delete old_data,也不成,不知道为什么。请高人指点~~!
谢谢你!
11 回复
#2
踏魔狼2007-02-16 00:13
不知道你这个old_data在这里有什么作用!再着它只是一个指针没有用new来分配内存,为什么要用delete来对付它呢!
#3
踏魔狼2007-02-16 00:38

#include "iostream.h"
#include "stdio.h"

int main(){
char* data;
char* new_data;
char* old_data;

old_data = data;
new_data = new char[20];
data =new_data;
delete old_data; // 这时一定会报错误,因为当时的data还没有用new来分配内存
return 0;
}

看第二种
int main(){
char* data;
char* new_data;
char* old_data;


new_data = new char[20];
data =new_data;
old_data = data; // 这时就不会报错误了.
delete old_data;
return 0;
}

#4
shower2007-02-16 12:42

我是这样想的:请看vector的初始化:
版本1:
SW_Vector<T>::SW_Vector(int n)//constructor, given size n
{
start=0;
finish=0;
end_of_storage=n;
data=new T(n);
}

版本2:
template<typename T>
SW_Vector<T>::SW_Vector(int n,const T& value)//constructor, given size n and the initializing value
{
fill_initialize(n,value);
}

plate<typename T>
void SW_Vector<T>::fill_initialize(int n, const T& value)//initializing and fill in the first value
{
start=0;//initializing
finish=1;
end_of_storage=n;
data=new T(n); //allocate
data[start]=value; //fill in
}

data是动态分配分配的!我们用data[0,n-1]来表示。我们再看push_back这个函数:
template<typename T>
void SW_Vector<T>::push_back(const T& x)//insert x at the end of the vector
{
if (!full())
{
data[finish++]=x;
}
else
{
int i;
T* new_data;
T* old_data;
old_data=data;
new_data=new T(2*size());
//copy the old values
for (i=start;i<finish;i++)
{
new_data[i]=data[i];
}
new_data[i]=x;

data=new_data;
finish++;
end_of_storage*=2;

//start remain the same
//delete
delete []old_data;
}
}
首先:data是需要delete的!push_back中的else部分是来处理空间不够的情况的!我先把old_data指向data,此时old_data便指向一个大小为n的空间。然后申请一个new_data指向一个两倍大的空间,然后进行复制。接着把data指向这个新申请的空间。然后需要delete原来data的空间,这片空间是用old_data来标记的,所以我就用了delete []old_data
以上是我的想法,我没有看出有什么错误。请你帮我指明!谢谢你!

#5
踏魔狼2007-02-16 13:14
template<typename T>
void SW_Vector<T>::push_back(const T& x)//insert x at the end of the vector
{
if (!full())
{
data[finish++]=x;
}
else
{
int i;
T* new_data;
//T* old_data;
//old_data=data;

new_data=new T(2*size());
//copy the old values
for (i=start;i<finish;i++)
{
new_data[i]=data[i];
}
new_data[i]=x;

delete[] data;
data=new_data;
finish++;
end_of_storage*=2;

//start remain the same
//delete
//delete []old_data;
}
}

#6
song42007-02-16 13:22
以下是引用shower在2007-2-16 12:42:00的发言:

我是这样想的:请看vector的初始化:
版本1:
SW_Vector<T>::SW_Vector(int n)//constructor, given size n
{
start=0;
finish=0;
end_of_storage=n;
data=new T(n);是这个吧[]
}

版本2:
template<typename T>
SW_Vector<T>::SW_Vector(int n,const T& value)//constructor, given size n and the initializing value
{
fill_initialize(n,value);
}

plate<typename T>
void SW_Vector<T>::fill_initialize(int n, const T& value)//initializing and fill in the first value
{
start=0;//initializing
finish=1;
end_of_storage=n;
data=new T(n); //allocate
data[start++]=value; //fill in
}//你这只给一个值,为什么不用PUSH完成呢,很费解啊,不如把这个函数改成初始化全部数组的函数

data是动态分配分配的!我们用data[0,n-1]来表示。我们再看push_back这个函数:
template<typename T>
void SW_Vector<T>::push_back(const T& x)//insert x at the end of the vector
{
if (!full())
{
data[finish++]=x;
}
else
{
int i;
T* new_data;
T* old_data;
old_data=data;
new_data=new T(2*size());
//copy the old values
for (i=start;i<finish;i++)
{
new_data[i]=data[i];
}
new_data[i]=x;

data=new_data;
finish++;
end_of_storage*=2;

//start remain the same
//delete
delete []old_data;
}
}
首先:data是需要delete的!push_back中的else部分是来处理空间不够的情况的!我先把old_data指向data,此时old_data便指向一个大小为n的空间。然后申请一个new_data指向一个两倍大的空间,然后进行复制。接着把data指向这个新申请的空间。然后需要delete原来data的空间,这片空间是用old_data来标记的,所以我就用了delete []old_data
以上是我的想法,我没有看出有什么错误。请你帮我指明!谢谢你!

//思路很对啊

#7
踏魔狼2007-02-16 13:32
的确!没看见它是用()的!
#8
song42007-02-16 13:35
呵呵
你还上学么??
楼上,很想认识你

[此贴子已经被作者于2007-2-16 13:49:35编辑过]

#9
踏魔狼2007-02-16 13:50
你说我吗?
#10
song42007-02-16 13:57

呵呵
老人了 ,却一直幕后
#11
踏魔狼2007-02-16 15:36
早八百年前就没有读书啦.
#12
shower2007-02-16 16:13

哦,谢了哦,踏魔狼的的确更好。
谢谢~~~!

1