C 過載流運算子

2021-09-10 02:50:52 字數 1714 閱讀 5113

(100/100 分數)

題目描述

實現乙個學生類,包含學號(id),姓名(name),年級(grade)資料成員。為了輸入輸出的方便,需要對這個學生類過載「>>」和「<<」運算子,同時為了對多個學生按照學號從小到大排序,還需要過載「<」運算子,以使用stl裡的sort函式。類的宣告與主函式的測試**已給出,請將類的實現與相關過載補充完整,使得程式正確執行並輸出正確結果。

#include #include #include #include using namespace std;

class 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);

/*請在這裡填充***/

int main()

sort(sv.begin(), sv.end());

for (int i = 0; i < sv.size(); ++i)

cout << sv[i];

return 0;

}

輸入描述

每個測例有多行,每行包含乙個整數 id 表示學號,字串 name 表示姓名(中間無空格),字串 grade 表示年級,三者用空格隔開。

輸出描述

將輸入的所有學生按照學號由小到大排序後,按順序輸出,每個學生佔一行,格式與輸入相同。

樣例輸入

3 lilei 14

2 hanmeimei 14

1 lintao 15

樣例輸出

1 lintao 15

2 hanmeimei 14

3 lilei 14

#include #include #include #include using namespace std;

class 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);

/*請在這裡填充***/

bool student::operator < (const student & s) const

ostream & operator << (ostream & out, student &s)

int main()

sort(sv.begin(), sv.end());

for (int i = 0; i < sv.size(); ++i)

cout << sv[i];

return 0;

}

過載流插入運算子《和流提取運算子》

includeclass complex complex double r,double i complex operator complex c2 friend ostream operator ostream output,complex c private double real double...

過載流插入運算子和流提取運算子

有兩個矩陣a和b,均為2行3列。求兩個矩陣之和。過載運算子 使之能用與矩陣相加,如c a b。過載流插入運算子 和流提取運算子 使之能用與該矩陣的輸入和輸出,如cin a,cout 我是剛學習c de菜鳥 源 如下 include using namespace std class matrix m...

過載流插入運算子

使用者自定義的型別的資料,是不能直接用 和 來輸出和輸入的。如果想用他們輸出和輸入自己宣告的型別的資料,必須對他們進行過載。和 過載函式形式如下 istream operator istream 自定義型別 ostream operator ostream 自定義型別 注意 只能將過載 和 的函式作...