OPP实验六

任务一、

 对于约束性模板友元和非约束性模板友元,在语法上的区别在于,templete<typename T>T的名字与类中是不一样就是非约束性

就是不受类的模板的影响相互独立的。

由于这里出现了两种的输出方式,通过 ostream是iostream和fostream 的基类,就可以一个重载使用两个,方便了一点。

任务二、

 这里主要是对文件的输入输出的操作,重载了>>和<<,来对vector<class>进行输入输出。这也许是学到的最重要的地方

任务三、

 

 

 

抛出了一个invalid_argument类,用exception类接住,其次有这个声明,invalid_argument是exception 的子类所以可以被接住,表示这一类错误吧,而exception的基类是logic_error类

 

最后就是文件结尾的检测

 任务四、

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <stdexcept>
 5 using namespace std;
 6 
 7 template <typename T>
 8 class Vector{
 9 private:
10   int n;
11   T* Array;
12 public:
13   Vector(int nt,T value=0){
14     if(nt<0){
15         throw length_error("Vector length must be >= 0"); 
16     }
17     if(value<0){
18         throw length_error("Value must be >= 0");
19     }
20       Array=new T(nt);
21       n=nt;
22 
23       for(int i=0;i<n;i++){
24         Array[i]=value;
25       }
26   }
27 
28   ~Vector(){
29     delete[] Array;
30   }
31 
32   int get_size(){
33     return n;
34   } 
35 
36   T& at(int index){
37     if(index<0 || index>=n){
38         throw out_of_range("Index out of range");
39     }
40     return Array[index];
41   }
42 
43   T& operator[](int index){
44     if(index<0 || index>=n){
45         throw out_of_range("Index out of range");
46     }
47     return Array[index];
48   }
49 
50   friend void output(Vector<T> v){
51     for(int i=0;i<v.n;i++){
52         cout<<v[i]<<" ";
53     }
54     cout<<endl;
55   }
56 
57 };

 

 任务五、

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <fstream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 class infor{
 8 private:
 9   string number;
10   string name;
11   string acedemic;
12   int grade;
13 public:
14   
15   string get_acedemic()
16   {return acedemic;}
17 
18   int ger_grade()
19   {return grade;}
20 
21   friend ostream& operator<<(ostream& out,infor& inf) 
22   {
23     out<<"  "<<inf.number<<"  "<<inf.name<<"  "<<inf.acedemic<<"  "<<inf.grade<<endl;
24     return out;
25   }
26 
27   friend istream& operator>>(istream& in,infor& inf)
28   {
29     in>>inf.number>>inf.name>>inf.acedemic>>inf.grade;
30     return in;
31   }
32 
33 };
34 
35 void Get_file(string filename,vector<infor>& vec)
36 {
37     ifstream file(filename);
38 
39     if(!file.is_open())
40     {
41         cout<<"file open failed"<<endl;
42         return ;
43     }
44     string fist_line;
45     getline(file,fist_line);
46     infor information;
47 
48     
49     while(file>>information)
50     {
51         vec.push_back(information);
52         cout<<information<<endl;
53     }
54 
55     file.close();
56 }
57 
58 void save(string filename,vector<infor>& vec)
59 {
60     ofstream file(filename);
61 
62     if(!file.is_open())
63     {
64         cout<<"file open failed"<<endl;
65         return ;
66     }
67     for(long long int i=0;i<vec.size();i++)
68     file<<vec[i];
69 
70     file.close();    
71 }
72 
73 bool compare(infor& inf1,infor& inf2)
74 {
75     if(inf1.get_acedemic()>inf2.get_acedemic())return true;
76 
77     else if(inf1.get_acedemic()<inf2.get_acedemic())return false;
78 
79     return inf1.ger_grade()>inf2.ger_grade();
80 }
81 
82 
83 int main()
84 {
85     vector<infor> MyVector;
86 
87     Get_file("C:\\vscodemood\\cpp\\cpp\\src\\example.txt",MyVector);
88 
89     cout<<MyVector[0];
90 
91     sort(MyVector.begin(),MyVector.end(),compare);
92 
93     save("ans.txt",MyVector);
94 }

 

 在代码中重载了<<和>>,使用的输出流类是ostream和istream,这是ofstream和ifstream的基类可以一起使用

将数据存储在vector中,方便管理

遇到的一个问题是输入老是是默认值,后来发现是没有使用引用类型,也是非常的无语。。。。。

 

posted @ 2024-12-17 17:27  Mthe  阅读(17)  评论(0)    收藏  举报