gpt4 book ai didi

c++ - 返回结构指针

转载 作者:行者123 更新时间:2023-11-28 02:00:48 25 4
gpt4 key购买 nike

我对 C++ 很陌生,需要一些有关以下代码的帮助。

#include <iostream>
using namespace std;

struct student {
string name;
int age;
float marks;
};

struct student *initiateStudent(string , int , float);

int main ( ) {
int totalStudents = 1;
string name;
int age;

float marks;

cin >> totalStudents;

student *stud[totalStudents];

for( int i = 0; i < totalStudents; i++ ) {
cin >> name >> age >> marks;
stud[i] = initiateStudent(name,age,marks);
}
cout << stud[0]->name;

return 0;
}

struct student *initiateStudent(string name, int age, float marks)
{
student *temp_student;

temp_student->name = name;
temp_student->age = age;
temp_student->marks = marks;
return temp_student;
}

我需要在函数 initiateStudent 中返回一个指向指针数组 stud 的结构指针,方法是传递成员 nameage, 标记。我知道到目前为止的问题是当我返回主文件时 temp_student 被破坏了。所以我的问题是如何通过仅传递结构的成员然后将信息返回给指针数组 stud 来完成。

非常感谢。

最佳答案

半答案解释坏习惯:

#include <string>
#include <iostream>
#include <vector>

//using namespace std; often injects subtle bugs. Use with caution
// read more here:
// http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice


struct student
{
std::string name; // explicit namespacing reduces possibility of unwanted collisions
int age;
float marks;
//added constructor in place of initialization function.
student(std::string name, int age, float marks):name(name), age(age), marks(marks)
{
}
};

int main()
{
int totalStudents = 1;
std::string name;
int age;

float marks;

while (!(std::cin >> totalStudents)) // testing input for success
// Needed extra brackets caught by M.M
// teach me to not test even a throw-away example
{
std::cout << "must... have... good... input..." << std::endl;
cin.clear(); // clear the error and get rid of any other garbage the user may have input.
cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

//student *stud[totalStudents]; illegal in C++
std::vector<student *> stud(totalStudents); // using dynamic array instead

for (int i = 0; i < totalStudents; )// i++ removed
{
if (std::cin >> name >> age >> marks) //testing input
{
stud[i] = new student(name, age, marks); // using constructor
i++; // and put here. Only increment if input is valid.
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
std::cout << stud[0]->name;

for (student * stu: stud) // cleaning up allocated memory
{
delete stu;
}

return 0;
}

C++ 的优点之一是您很少需要自行管理内存。其实还有huge advantages in not doing it更重要的是不必自己清理。

#include <string>
#include <iostream>
#include <vector>


struct student
{
std::string name;
int age;
float marks;

student(std::string name, int age, float marks):name(name), age(age), marks(marks)
{
}
};

int main()
{
std::string name;
int age;
float marks;

std::vector<student> stud; // look ma! No pointer!

while (std::cin >> name >> age >> marks) //exits loop on bad input
{
stud.emplace_back(name, age, marks); // building directly in vector
// vector will size itself as needed.
}
std::cout << stud[0].name;

return 0;
}

还有一个警告:>> 是空格分隔的。这意味着它会在找到空格(空格、制表符、行尾...)时停止,因此名称“John Jacob Jingleheimer-Shmidt”将以“John”的形式出现。 >>> 将尝试将“Jacob”解释为 age,这不会很顺利。

关于c++ - 返回结构指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com