Home > Back-end >  How to determine an array of a particular object is populated
How to determine an array of a particular object is populated

Time:10-24

If there is a structure, and a print function
 
Struct Student {
std::string name;
int age;
};

Void printStudent (const Student * students, STD: : siez_t len) {
For (STD: : size_t I=0; I & lt; Len. + + I) {
//how to determine (students + I) is not null
STD: : cout<" The first "& lt; STD: : cout<" The name "& lt; <(students + I) - & gt; Name<" Age "& lt; <(students + I) - & gt; Age{
}


Program calls printStuden () function, the incoming array may be new and no assignment, similar to the following
 
Student * class1Student=new Student [MAX_COUNT];
PrintStudent (class1Student MAX_COUNT);

Like this kind of new structure array, how to determine the structure have an element of an array of assigned values, with (student + I)==nullptr line?

CodePudding user response:

Can be judged (students + I) - & gt; If the name is empty, the assignment name should not is empty? The best practice is to give the struct Student write a no default constructor, set in the constructor on the business name and age is not a legal value, and then you judge whether the value legal cycle, just know to have been filled, such as the age should not be negative? You're in the constructor set age=1, circulation in judging if age==1, it is illegal to bai, basic is not filled, unless you use the default Student to fill
  • Related