This article involves knowledge points: the definition of structure, the use of vector, the use of iterator, the avoidance of wild iterator and the method of sorting. If you need to do a course design such as student management system, this article will be a very important foundation. On this basis, you can easily complete the design of student management system.
The article involves the last question of the interview of a well-known Internet manufacturer. The article has detailed function implementation and comments. At the end of the article, there is a complete runnable code of the whole project. If you think it's good, please give the blogger a key three links + comment area interaction. Thank you
Title Requirements:
Define the structure of a Student, including two members: name and age. Use vector to manage the Student object.
1. Implement the init function and put three student elements ("Siri", 17), ("Xiaoai", 20), ("Xiaoyi", 19) into the vector;
2. Implement sort_ The student function sorts all student objects in the vector according to their age from small to large
3. Implement add_ The student function allows external calls to add one student and one ("Siri", 18) to the vector
4. Implement the delete function, which allows external calls to delete all students with the specified name and all students with the name Siri
Start answering:
Define structure:
typedef struct _STUDENT_ { string name;//full name int age;//Age }STUDENT, *PSTUDENT;
Let's define a structure. By default, it includes two members, one is name, the other is age, the name is string type, and the age is int type.
Complete the first requirement, implement the init function, and complete the initialization of three members:
/* Function is used to initialize three members and store them in the vector array Parameter: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument */ void Init(vector<STUDENT>& Parameter) { STUDENT student_1[3];//Used to store the information of three students student_1[0].name = "Siri"; student_1[0].age = 17; student_1[1].name = "Xiaoai"; student_1[1].age = 20; student_1[2].name = "Xiaoyi"; student_1[2].age = 19; for (int i = 0; i < 3; i++) { Parameter.push_back(student_1[i]);//Push parameters into vector array } }
We use the above functions to initialize the vector array. Note that the parameter is a reference type, which means that the change of the formal parameter will directly affect the value of the argument.
Then we implement sort_ For student, the members in the vector array are sorted from small to large according to the age size:
/* Function to sort the vector array Parameter: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument */ void sort_student(vector<STUDENT>& Parameter) { //sort function is a function under the algorithm library, which is used to realize the function of comparison and sorting sort(Parameter.begin(), Parameter.end(), compare);//Note that the third argument to this function is the function pointer } /* The function is used to compare the age of the and return the comparison result Parameter 1: const student & A is a constant reference Parameter 2: const student & A is a constant reference Return value: bool type, which is true or false */ bool compare(const STUDENT& a, const STUDENT& b) { return a.age < b.age; }
We use the sort function in algorithm to compare and sort the age. After this function is executed, all members are sorted according to their age.
Implement add_ According to the requirements of student, add a new student member ("Siri", 18) to the vector. The code is as follows:
/* Function to add external variables and add new members to the vector array through the passed in parameters Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* name constant character pointer, which is used to transmit the name of new external members Parameter 3: int age, enter the age of the new member externally */ void add_student(vector<STUDENT>& Parameter, const char* name,int age) { STUDENT Student; Student.name = name; Student.age = age; Parameter.push_back(Student); sort_student(Parameter);//After adding the students' information, sort again }
After passing in the student information that needs to be added by passing parameters, we declare the local structure variable in it to store this information. Finally, we press it into the vector array and sort the students again.
Implement the delete function to allow external calls to delete all student s named Siri:
/* Function to delete the specified students, search for matches through their names, and finally delete them Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* name constant character pointer, used to store the student name to be deleted Return value: bool type to tell the caller whether the deletion is successful */ bool delete_student(vector<STUDENT>& Parameter, const char* name) { bool IsOk = false; //The for loop involves the use of vector iterators, that is, vector < student >:: iterator v1 //Be careful with the occurrence of "wild" iterators, so I put the + + operation of the for loop in the ifelse statement for (vector<STUDENT>::iterator v1 = Parameter.begin(); v1 != Parameter.end();) { //Compare whether the name is consistent with what we want to delete if (strcmp((*v1).name.c_str(), name) == 0) { IsOk = true; //What is returned is an element after deletion v1 = Parameter.erase(v1);//Data erasure //After being erase, it becomes a "wild iterator", and its + + operation will break down } else { v1++; } } return IsOk; }
In fact, the implementation of delete is the most needed place, because it involves the problem of wild iterators. After using vector::erase, it will return the next iterator of the current array as the return value. Because the erase operation has become ineffective, the current iterator is a wild iterator, and direct + + operation will lead to program crash.
In fact, the basic requirements of the interview questions have been completed, and then I wrote a function of modification and query. After all, since I wrote addition and deletion, I'll write modification and query by the way. In fact, compared with deletion, modification and query are still a little similar. Without more words, let's look at a code for modifying information first:
bool change_student(vector<STUDENT>& Parameter, const char* old_name, const char* new_name = "default", int new_age = 0); /* change_student The function is to modify the previously stored information. If the student's name is wrong, it can be modified, and if the age is wrong, it can also be modified Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* old_name name of the student who needs information Parameter 3: const char * NEW_ The name needs to be changed to a new name. The default value is default Parameter 4: int new_age needs to be modified to the new age. The default value is 0 Return value: bool type to tell the caller whether the deletion is successful */ bool change_student(vector<STUDENT>& Parameter, const char* old_name, const char* new_name, int new_age) { bool IsOk = false; for (vector<STUDENT>::iterator v1 = Parameter.begin(); v1 != Parameter.end();) { if (strcmp((*v1).name.c_str(), old_name) == 0) { IsOk = true; if (strcmp(new_name, "default") != 0) { //Check whether the name needs to be modified. If it is not default, it needs to be modified (*v1).name = new_name; } if (new_age != 0) { //See whether the age needs to be modified. If it is not 0, it needs to be modified (*v1).age = new_age; } v1++;//Iterator backward } else { v1++; } } return IsOk; }
In the above code, I listed the declaration and definition of the function. During the declaration, the default value of the parameter appears. The default assignment of the parameter means that if these parameters are not passed, the default value can be passed in.
Finally, another function to find student information according to the student's name to complete the simple work of our code:
/* Function realizes the search of student information, searches for matches through names, and finally outputs the results Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* name constant character pointer, which is used to store the student name to be searched */ void find_student(vector<STUDENT>& Parameter, const char* name) { bool IsOk = false; for (vector<STUDENT>::iterator v1 = Parameter.begin(); v1 != Parameter.end();) { //Compare the name with what we are looking for if (strcmp((*v1).name.c_str(), name) == 0) { cout << "Student Name is " << name << endl; cout << "Student age is " << (*v1).age << endl; v1++;//Iterator backward } else { v1++; } } }
It is very simple to pass in the student's name from the outside, then use the iterator to traverse the vector array, judge the match, and then output the student's information.
The complete runnable code is as follows:
//Header file main h #pragma once #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; typedef struct _STUDENT_ { string name; int age; }STUDENT, *PSTUDENT; void Init(vector<STUDENT>& Parameter); void add_student(vector<STUDENT>& Parameter, const char* name, int age); bool delete_student(vector<STUDENT>& Parameter, const char* name); void sort_student(vector<STUDENT>& Parameter); bool compare(const STUDENT& a, const STUDENT& b); bool change_student(vector<STUDENT>& Parameter, const char* old_name, const char* new_name = "default", int new_age = 0); void find_student(vector<STUDENT>& Parameter, const char* name);
//cpp main.cpp #include"main.h" /* Function is used to initialize three members and store them in the vector array Parameter: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument */ void Init(vector<STUDENT>& Parameter) { STUDENT student_1[3];//Used to store the information of three students student_1[0].name = "Siri"; student_1[0].age = 17; student_1[1].name = "Xiaoai"; student_1[1].age = 20; student_1[2].name = "Xiaoyi"; student_1[2].age = 19; for (int i = 0; i < 3; i++) { Parameter.push_back(student_1[i]);//Push parameters into vector array } } /* Function to sort the vector array Parameter: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument */ void sort_student(vector<STUDENT>& Parameter) { //sort function is a function under the algorithm library, which is used to realize the function of comparison and sorting sort(Parameter.begin(), Parameter.end(), compare);//Note that the third argument to this function is the function pointer } /* The function is used to compare the age of the and return the comparison result Parameter 1: const student & A is a constant reference Parameter 2: const student & A is a constant reference Return value: bool type, which is true or false */ bool compare(const STUDENT& a, const STUDENT& b) { return a.age < b.age; } /* Function to add external variables and add new members to the vector array through the passed in parameters Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* name constant character pointer, which is used to transmit the name of new external members Parameter 3: int age, enter the age of the new member externally */ void add_student(vector<STUDENT>& Parameter, const char* name,int age) { STUDENT Student; Student.name = name; Student.age = age; Parameter.push_back(Student); sort_student(Parameter);//After adding the students' information, sort again } /* Function to delete the specified students, search for matches through their names, and finally delete them Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* name constant character pointer, used to store the student name to be deleted Return value: bool type to tell the caller whether the deletion is successful */ bool delete_student(vector<STUDENT>& Parameter, const char* name) { bool IsOk = false; //The for loop involves the use of vector iterators, that is, vector < student >:: iterator v1 //Be careful with the occurrence of "wild" iterators, so I put the + + operation of the for loop in the ifelse statement for (vector<STUDENT>::iterator v1 = Parameter.begin(); v1 != Parameter.end();) { //Compare whether the name is consistent with what we want to delete if (strcmp((*v1).name.c_str(), name) == 0) { IsOk = true; //What is returned is an element after deletion v1 = Parameter.erase(v1);//Data erasure //After being erase, it becomes a "wild iterator", and its + + operation will break down } else { v1++; } } return IsOk; } /* change_student The function is to modify the previously stored information. If the student's name is wrong, it can be modified, and if the age is wrong, it can also be modified Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* old_name name of the student who needs information Parameter 3: const char * NEW_ The name needs to be changed to a new name. The default value is default Parameter 4: int new_age needs to be modified to the new age. The default value is 0 Return value: bool type to tell the caller whether the deletion is successful */ bool change_student(vector<STUDENT>& Parameter, const char* old_name, const char* new_name, int new_age) { bool IsOk = false; for (vector<STUDENT>::iterator v1 = Parameter.begin(); v1 != Parameter.end();) { if (strcmp((*v1).name.c_str(), old_name) == 0) { IsOk = true; if (strcmp(new_name, "default") != 0) { //Check whether the name needs to be modified. If it is not default, it needs to be modified (*v1).name = new_name; } if (new_age != 0) { //See whether the age needs to be modified. If it is not 0, it needs to be modified (*v1).age = new_age; } v1++;//Iterator backward } else { v1++; } } return IsOk; } /* Function realizes the search of student information, searches for matches through names, and finally outputs the results Parameter 1: vector < student > & parameter is a reference to an external parameter. Change this parameter, That is, change the external argument Parameter 2: const char* name constant character pointer, which is used to store the student name to be searched */ void find_student(vector<STUDENT>& Parameter, const char* name) { bool IsOk = false; for (vector<STUDENT>::iterator v1 = Parameter.begin(); v1 != Parameter.end();) { //Compare the name with what we are looking for if (strcmp((*v1).name.c_str(), name) == 0) { cout << "Student Name is " << name << endl; cout << "Student age is " << (*v1).age << endl; v1++;//Iterator backward } else { v1++; } } } int main() { vector<STUDENT> Student; Init(Student);//Initialize the information of three students sort_student(Student); add_student(Student, "Siri",18); delete_student(Student, "Siri"); change_student(Student, "Xiaoai","Xiaobu"); find_student(Student, "Xiaoyi"); return 0; }
If you like the blogger's article, after paying attention, we will get along slowly.
Today's inspirational sentence "you make a choice. Don't look back."