代码分析(!!!!!!重要!!!!!)

正确代码:

#include <iostream> 
#include <vector>
#include <string>
//(1) 创建vector类型的容器,用于存放学生信息;
//
//(2) 创建3个学生(值自定义),放入向量中存储;
//
//(3) 编写函数计算所有学生的平均成绩;
//
//(4) 在向量中增加一个学生;
//
//(5) 从向量中删除第3个学生。
using namespace std;
struct stu
{
	string name;
	string sex;
	string no;
	float score;
}*student;
void creatVector(struct stu* student, vector <stu*>&pv);
void print_vector(vector<stu*> v);
float ave_score(vector<stu*>&pv);
int stu_num = 3;
vector <stu*> v ;
vector<stu*>* pv = &v;
int main()
{
	creatVector(student,v);
	print_vector(v);
	cout << "学生成绩的平均值:" << ave_score(v) << endl;
	
}
void print_vector(vector<stu*> v)
{
	for (vector<stu*>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << (*it)->name << "\t" << (*it)->no << "\t" << (*it)->score << "\t" << (*it)->sex << endl;
	}
	//cout << v[0]->name << " " << v[0]->no << " " << v[0]->score << v[0]->sex << endl;
}
void creatVector(struct stu* student, vector <stu*> &pv)
{
	int stu_num = 3;
	for (int i = 0; i < stu_num; i++)
	{
		stu* student = new stu();
		cout << "输入姓名、性别、学号、成绩\n";
		cin >> student->name >> student->sex >> student->no >> student->score;
		pv.push_back(student);
	}
}
float ave_score(vector<stu*>& pv)
{
	float sum = 0;
	float ave;
	for (vector<stu*>::iterator it = pv.begin(); it != pv.end(); it++)
	{
		sum += (*it)->score;
	}
	ave = sum / pv.size();
	return ave;
}

错误代码:

#include <iostream> 
#include <vector>
#include <string>
//(1) 创建vector类型的容器,用于存放学生信息;
//
//(2) 创建3个学生(值自定义),放入向量中存储;
//
//(3) 编写函数计算所有学生的平均成绩;
//
//(4) 在向量中增加一个学生;
//
//(5) 从向量中删除第3个学生。
using namespace std;
struct stu
{
	string name;
	string sex;
	string no;
	float score;
}*student;

void print_vector(vector<stu*> v);
void creatVector(struct stu* student, vector<stu*> v );
vector <stu*> v;//WHY  V[3]  BUXING ???
int stu_num = 0;
int main()
{
	creatVector(student,v);
	print_vector(v);
}
void print_vector(vector<stu*> v)
{
	for (vector<stu*>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << (*it)->name << "\t" << (*it)->no << "\t" << (*it)->score << "\t" << (*it)->sex << endl;
	}
}
void creatVector(struct stu *student,vector<stu*> v)
{
	student = new stu();
	 stu_num = 3;
	for (int i = 0; i < stu_num; i++)
	{
		student->name = "name" + to_string(i);
		student->no = to_string(i*111111111);
		student->sex = "男";
		student->score = i * 67;
		v.push_back(student);
	}
	for (vector<stu*>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << (*it)->name << "\t" << (*it)->no << "\t" << (*it)->score << "\t" << (*it)->sex << endl;
	}
}

错误显示:

这段代码中存在以下错误:

  1. creatVector函数中,student是一个指针,但是在函数内部又被重新定义为一个stu类型的对象。这会导致原来的指针丢失,无法正确访问到向量中的学生信息。

  2. main函数中,调用creatVector函数时,传入的参数是studentv,但是student并没有被初始化,它的值是不确定的。

  3. creatVector函数中,创建了一个新的stu对象并赋值给student,但是这个对象并没有被添加到向量中,所以向量中只有一个元素。

  4. creatVector函数中,使用了new关键字来动态分配内存,但是没有使用delete关键字来释放内存,这会导致内存泄漏。

  5. print_vector函数中,使用了迭代器来遍历向量,但是在循环体中又使用了箭头运算符来访问成员变量,这是不正确的

分析总结:

1.

2.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小布丁729

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值