STL,list 调试,帮看看错在哪?

来源:百度知道 编辑:UC知道 时间:2024/06/30 13:09:51
#include <list>
#include <iostream>
using namespace std;
struct Item
{
int code;
int lev;
}MyItem;
list<Item*>::iterator k;
list<Item*>GamePack;
k=GamePack.begin();
void main()
{
for (int i=0; i<3; i++)
{
cout<<"input item!"<<endl;
cin>>MyItem.code;
cin>>MyItem.lev;
GamePack.push_back(MyItem);
}
for (int i=0; i<3; i++)
{

cout<<*(k->code)<<","<<*(k->lev)<<endl;
k++;

}

}
这样得,但还是有错,是不是NEW了还要DELETE?
#include <list>
#include <iostream>
using namespace std;
struct Item
{
int code;
int lev;
};
list<Item*>::iterator k;
list<Item*>GamePack;
Item *MyItem = new Item;
void main()
{
for (int i=0; i<3; i++)
{
cout<

#include<iostream>
using namespace std;
//#include<stdio.h>
//#include<stdlib.h>

#include <list>

struct Item
{
int code;
int lev;
};
list<Item >::iterator k;
list<Item >GamePack;
Item MyItem ;
void main()
{

for (int i=0; i<2; i++)
{
cout<<"input item!"<<endl;
cin>>MyItem.code;
cin>>MyItem.lev;
GamePack.push_back(MyItem);
}
k = GamePack.begin();
for (int i=0; i<2; i++)
{
cout <<"-------------" <<endl;
cout<<(k)->code<<","<<(k)->lev<<endl;
k++;
}
int i;
cin >>i;
}

指针不是用着爽就用的,如果你需要3个元素,那么就要new3次,写起来反而麻烦

C++里面最麻烦的就是这个了,你创建了一个存储单元就必须记者把它释放掉,不释放就会造成内存泄漏。应该有Delete(*MyItem)。

晕了,说说问题吧,
1、最上面的那个MyItem是一个结构,你要把它实体化了才能赋值,<