请教vector的erase()产生的问题。。急阿

tingst 2005-12-31 05:53:05
class CSE
{
public:
~CSE();
CSE(int Var_Num);
   int Var_N;
int* State;
int Level;
};
CSE::CSE(int Var_Num)
{
Var_N = Var_Num;
State = new int[Var_N+1];
for (int i=0;i<Var_N+1;i++)
{
State[i] = 0;
}
Level=1;

}

CSE::~CSE()
{
if (State != NULL)
{
delete []State;
State = NULL;
}

}
void main()
{
vector(CSE) TSP1;
CSE SE1(5);
CSE SE2(5);
TSP1.push_back(SE1);
TSP1.push_back(SE2);
TSP1.erase(TSP1.begin());
for (int i=0;i < TSP1.size();i++)
{
cout<<TSP1[i].State[0]<<endl;
}

}

问题出在删除TSP1.begin()元素以后,输出结果为非0;不知道怎么解决,望高手指点,最好能给出提示性的语句。谢谢
...全文
387 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
tingst 2006-01-03
  • 打赏
  • 举报
回复
多谢大家的帮助,^_^
rr12 2006-01-02
  • 打赏
  • 举报
回复
//使用vector<CSE> TSP1;也行要使对象能够拷贝和赋值。

CSE::CSE(const CSE &other)
{
Var_N = other.Var_N;
State = new int[Var_N];

for(int i=0; i<Var_N; ++i)
{
State[i] = other.State[i];
}

}


CSE& CSE::operator=(const CSE &other)
{
if(this != &other)
{
delete []State;
Var_N = other.Var_N;
State = new int[Var_N];

for(int i=0; i < Var_N ; ++i)
{
State[i] = other.State[i];
}
}

return *this;

}
rr12 2006-01-02
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>

using namespace std;


class CSE
{
public:
CSE(int Var_Num);
CSE();
virtual ~CSE();

int Var_N;
int* State;
int Level;


};

CSE::~CSE()
{
if (State != NULL)
delete []State;
State = NULL;

}

CSE::CSE(int Var_Num)
{
Var_N = Var_Num;
State = new int[Var_N];
for (int i=0; i < Var_N; i++)
{
State[i] = 0;
}
Level=1;

}

void main() {

vector<CSE *> TSP1;

TSP1.push_back(new CSE(5));
TSP1.push_back(new CSE(5));

//delete first element and free the memory
vector<CSE *>::iterator itNum = TSP1.begin();
delete *itNum;
TSP1.erase(itNum);

//view other elements
itNum = TSP1.begin();
for (; itNum < TSP1.end(); itNum++)
cout<< (*itNum)->State[0] <<endl;

//free memory and clear vector
itNum = TSP1.begin();
for (; itNum < TSP1.end(); itNum++)
delete *itNum;

TSP1.clear();

}
rr12 2006-01-01
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>

using namespace std;


vector<CSE *> TSP1;
CSE SE1(5);
CSE SE2(5);


TSP1.push_back(&SE1);
TSP1.push_back(&SE2);

vector<CSE *>::iterator itNum = TSP1.begin();
TSP1.erase(itNum);

itNum = TSP1.begin();
for (; itNum < TSP1.end(); itNum++)
cout<< (*itNum)->State[0] <<endl;


TSP1.clear();
sakaer 2006-01-01
  • 打赏
  • 举报
回复
楼上的办法好啊..........
xlsue 2006-01-01
  • 打赏
  • 举报
回复
//我帮你改写了一下,不用自己管理内存,让它自己操心去吧。。。

#include <iostream>
#include <vector>
using namespace std;

class CSE
{
public:
~CSE();
CSE(int Var_Num);
int Var_N;
vector<int> State;
int Level;
};
CSE::CSE(int Var_Num):Var_N(Var_Num),State(Var_N)
{
Level=1;
}

CSE::~CSE()
{

}

int main()
{
vector<CSE> TSP1;
CSE SE1(5);
CSE SE2(5);
TSP1.push_back(SE1);
TSP1.push_back(SE2);
TSP1.erase(TSP1.begin());
for (int i=0;i < TSP1.size();i++)
{
cout<<TSP1[i].State[0]<<endl;
}

getchar();
return 0;
}
xlsue 2006-01-01
  • 打赏
  • 举报
回复
晕,没注意看楼上写出来了,我又写个,汗。。。
sankt 2005-12-31
  • 打赏
  • 举报
回复
汗一个。。
上面有笔误

涉及到动态内存分配,务必写好赋值构造函数 和 赋值重载函数
//============
涉及到动态内存分配,务必写好拷贝构造函数 和 赋值重载函数
sankt 2005-12-31
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>

using namespace std;


class CSE
{
public:
~CSE();
CSE(int Var_Num);
CSE(const CSE &other)
{
Var_N=other.Var_N;
int i;
State=new int[Var_N];

for(i=0;i<Var_N;++i)
{
State[i]=0;
}


}

CSE& operator=(const CSE &other)
{
if(this!=&other)
{
delete []State;
Var_N=other.Var_N;
int i;
State=new int[Var_N];

for(i=0;i<Var_N;++i)
{
State[i]=0;
}
}
return *this;
}



int Var_N;
int* State;
int Level;

};

CSE::CSE(int Var_Num)
{
Var_N = Var_Num;
State = new int[Var_N+1];

for (int i=0;i<Var_N+1;i++)
{
State[i] = 0;
}
Level=1;

}

CSE::~CSE()
{

if (State != NULL)
{
delete []State;

}
State = NULL;
}

int main()
{
vector<CSE> TSP1;
CSE SE1(5);
CSE SE2(5);
CSE SE3(5);


TSP1.push_back(SE1);
TSP1.push_back(SE2);
TSP1.push_back(SE3);

TSP1.erase(TSP1.begin());

int i;

for (i=0;i < TSP1.size();i++)
{
cout<<TSP1[i].State[0]<<endl;
}
system("pause");
return 0;


}

//=================
涉及到动态内存分配,务必写好赋值构造函数 和 赋值重载函数

以上程序,在dev-c++调试通过
睡在床板下_ 2005-12-31
  • 打赏
  • 举报
回复
哦,对啊, 楼上的 也是 好 方法哦~~
lz可以不用vector,用list 也行
oyljerry 2005-12-31
  • 打赏
  • 举报
回复
提供了拷贝构造就可以了
erase后,保存下一个iterator
oyljerry 2005-12-31
  • 打赏
  • 举报
回复
还要提供CSE的拷贝构造函数等
tingst 2005-12-31
  • 打赏
  • 举报
回复
如果不用在VECTOER中,我的析构函数是对的。那如果要满足各种情况的析构函数该如何实现。请各位高手赐教。
睡在床板下_ 2005-12-31
  • 打赏
  • 举报
回复
这个我不会呢,我写的 程序比较少,所以 这个问题我还真的没 仔细想过怎么写哦
tingst 2005-12-31
  • 打赏
  • 举报
回复
说明一下:
我为了说明方便,把代码中拷贝构造省掉了。原来有:
CSE::CSE(const CSE &SE)
{
Var_N = SE.Var_N;

State = new int[SE.Var_N+1];

for(int i=0;i<SE.Var_N+1;i++)
State[i] = SE.State[i];

}

TO:corrupt(MM你:貌赛西施,才过清照,千年难遇的齐女啊)
那不会谢漏的析构函数该怎么写呢?
cunsh 2005-12-31
  • 打赏
  • 举报
回复
不知道楼主这个程序是想作甚么的.
楼上的说了.class CSE 要在拷贝构造函数和赋值的函数中自己管理内存.
CSE SE1(5);
CSE SE2(SE1); //这样两个CSE对象的State指针指到了同一个内存了.如果其中一个析构后释放了内存另一个还使用就要出错了.
睡在床板下_ 2005-12-31
  • 打赏
  • 举报
回复
楼主 ,你的程序是因为 析构函数中 ,delete 出错的
------------------
因为,vector 在初始化时,分配空间为0,
接着 push_back 时,空间改变 为1。你的程序没问题

接着 push_back 时,空间 分配为2,由于先前只有1,所以要
空间配置器重新分配, 你的 new的地址指针 全错了,所以在最后
析构时,和原来的指针不相配,出错。。。。。。

lz可以 把析构函数去了, 程序就对了, 但是 内存会漏
tingst 2005-12-31
  • 打赏
  • 举报
回复
上面高手能否给出一点代码呢。以下是我的一种比较简陋的解决方法,不知道有没有更好的解决途径。
my method:
void main()
{
T_SE_Vector TSP1,TSP2;
CSE SE1(5);
CSE SE2(5);
TSP1.push_back(SE1);
TSP1.push_back(SE2);
TSP2 = TSP1;
TSP1.clear();
for (int j=0;j<TSP2.size()-1;j++)
{
TSP1.push_back(TSP2[j+1]);
}
TSP2.clear();
for (int i=0;i < TSP1.size();i++)
{
cout<<TSP1[i].State[0]<<endl;
}

int kk;
cin>>kk;
}
whyglinux 2005-12-31
  • 打赏
  • 举报
回复
类中有一个指针类型的数据成员,在构造函数中进行内存分配。这就要求你不能直接使用类的默认的拷贝构造函数和赋值拷贝函数——你需要给出其正确的实现,即正确处理对象拷贝时的内存分配问题。因为标准容器中的元素要求拷贝操作。
睡在床板下_ 2005-12-31
  • 打赏
  • 举报
回复
程序 出错在 析构函数处
加载更多回复(2)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧