Academy Online C + + Advanced Programming question 11-2

Source: Internet
Author: User

The

implements a student class that contains a school number (ID), name (name), and grade (grade) data member. In order to enter the convenience of the output, you need to overload the student class with the ">>" and "<<" operators, while in order to the number of students from small to large sorting, you also need to overload the "<" operator to use the STL in the sort function. The declaration of the class and the test code for the main function have been given, please supplement the implementation of the class with the related overload to make the program run correctly and output the correct result.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
 
Class Student
{public
:
       int id;
       string name, grade;
       Student () {};
       Student (int ID, string name, string grade);
       BOOL operator < (const Student & S) const;
 
IStream & operator >> (IStream & In, Student & S);
Ostream & operator << (Ostream & Out, Student & S);
 
/* Please fill in the code *
 
/INT main ()
{
       vector<student> sv;
       Student temp;
       while (CIN >> temp)
       {
              sv.push_back (temp);
       }
       Sort (Sv.begin (), Sv.end ());
       for (int i = 0; i < sv.size (); ++i)
              cout << sv[i];
       return 0;
}



Input description

Each sample has multiple lines, each containing an integer ID representing the number, the string name representing the name (no spaces in the middle), the string grade the grade, and the three separated by a space.



Output description

The input of all students by the school number from small to large sort, in order output, each student occupies a row, the format and input the same.



Sample input

3 Lilei
2 Hanmeimei
1 Lintao 15

Sample output

1 Lintao
2 Hanmeimei
3 Lilei 14





#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace Std;

Class Student
{
Public
int id;
string name, Grade;
Student ()
{
Id=-1;
Name= ' ";
Grade= ' ";
}
Student (int ID, string name, string grade)
{
this->id=id;
this->name=name;
this->grade=grade;
}
BOOL operator < (const Student & S) const
{
if (s.id>this->id)
return true;
Else
return false;
}
};

IStream & operator >> (IStream & In, Student & S)
{
cin>>s.id;
cin>>s.name;
cin>>s.grade;
return in;
}
Ostream & operator << (Ostream & Out, Student & S)
{
cout<<s.id<< "" <<s.name<< "" <<s.grade<<endl;
return out;
}

/* Please fill in the code here * *

int main ()
{
Vector<student> SV;
Student temp;
while (CIN >> temp)
{
Sv.push_back (temp);
}
Sort (Sv.begin (), Sv.end ());
for (int i = 0; i < sv.size (); ++i)
cout << Sv[i];
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.