首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

vector 对象数组删除有关问题

2013-02-15 
vector 对象数组删除问题刚学c++,以前写过一个vector对象数组,对vector用法不熟,现在想给他加一个删除和修

vector 对象数组删除问题
刚学c++,以前写过一个vector对象数组,对vector用法不熟,现在想给他加一个删除和修改的操作,对照网上一个修改但是还是报错,不知怎么改了,求指教

问题在红色标注地方

# include<iostream>
# include <string>
# include <vector>
using namespace std;
class Student
{public:
      Student()
  {number=0;
   score=0;
   num++;
  }
      void setdata(double a,char m[],double s);
  void disp();
  static void avg(void);
  double geta(void){return number;}
 private:
 double number,score;
 char name[20];
 static double sum,num;
};
double Student::sum=0;
double Student::num=0;

void Student::setdata(double number1,char m[],double score1)
{number=number1; 
strcpy(name,m);
score=score1;
        sum=sum+score;
}

void Student::disp()
{
cout<<number<<"     "<<name<<"     "<<score;
}

void Student::avg()
{
cout<<"平均成绩是: "<<sum/num;
}
int main()
{
    int i=0;
    double number=1,score=0;
    char name[20];
    cout<<"请输入学生的学号(按0结束输入),姓名,成绩:"<<endl;
    vector<Student>Stu;
    for(i=0;number!=0;i++)
    {
cin>>number;
if(number==0) break;
cin>>name>>score;
Student stu;
stu.setdata(number,name,score);
Stu.push_back(stu);
    }
for(i=0;i<Stu.size();i++)
{
Stu[i].disp();
cout<<endl;
}
cout<<endl;
cout<<"请输入要删除学号"<<endl;
double mn;
cin>>mn;
         for(int e=Stu.size()-1;e>=0;e--)
        if(Stu[i].geta() ==mn)
        {delete (Stu[e]);Stu.erase(Stu.begin()+e);}  
         Student::avg();
cout<<endl;
return 0;
}
[解决办法]


#include<iostream>
# include <string>
# include <vector>
using namespace std;
class Student
{
public:
Student()
{number=0;
score=0;
num++;
}
void setdata(double a,char m[],double s);
void disp();
static void avg(void);
double geta(void){return number;}
private:
double number,score;
char name[20];
static double sum,num;
};
double Student::sum=0;
double Student::num=0;

void Student::setdata(double number1,char m[],double score1)

number=number1; 
strcpy(name,m);
score=score1;
sum=sum+score;
}

void Student::disp()
{
cout<<number<<"     "<<name<<"     "<<score;
}

void Student::avg()


{
cout<<"平均成绩是: "<<sum/num;
}

int _tmain(int argc, _TCHAR* argv[])
{
int i=0;
double number=1,score=0;
char name[20];
cout<<"请输入学生的学号(按0结束输入),姓名,成绩:"<<endl;
vector<Student>Stu;
for(i=0;number!=0;i++)
{
cin>>number;
if(number==0) break;
cin>>name>>score;
Student stu;
stu.setdata(number,name,score);
Stu.push_back(stu);
}
for(i=0;i<Stu.size();i++)
{
Stu[i].disp();
cout<<endl;
}
cout<<endl;
cout<<"请输入要删除学号"<<endl;
double mn;
cin>>mn;
/*
for(int e=Stu.size()-1;e>=0;e--)
if(Stu[e].geta() ==mn)
{
Stu.erase(Stu.begin()+e);
break;
}  
*/
for(vector<Student>::iterator e = Stu.begin(); e != Stu.end(); e++)
{
if(e->geta() == mn)
{
Stu.erase(e);
break;
}
}
Student::avg();
cout<<endl;
system("pause");
return 0;
}


请输入学生的学号(按0结束输入),姓名,成绩:
1 aa 89
2 bb 79
3 cc 82
4 dd 93
5 ee 81
0
1     aa     89
2     bb     79
3     cc     82
4     dd     93
5     ee     81

请输入要删除学号
3
平均成绩是: 84.8
[解决办法]
对于你自己i的代码
1.
if(Stu[i].geta() ==mn)

改成
if(Stu[e].geta() ==mn)

2.
Stu.erase(Stu.begin()+e);

后添加break语句

热点排行