模板的简单使用(vector)
Posted 2015-16
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模板的简单使用(vector)相关的知识,希望对你有一定的参考价值。
上代码:
1 //用结构和STL常用算法实现对学生成绩的排序和查找。 2 #include<iostream> 3 #include<vector> 4 #include<algorithm> 5 #include<cstring> 6 using namespace std; 7 8 struct Student 9 { 10 string name; 11 string id; 12 double score; 13 Student(string name_ = " ",string id_ = " ",double score_ = 0): 14 name(name_),id(id_),score(score_) { } 15 bool operator == (const Student& rhs) 16 { 17 return score== rhs.score; 18 } 19 bool operator < (const Student& rhs) 20 { 21 return (score < rhs.score); 22 } 23 }; 24 25 void Printvector(const vector<Student> & vv) 26 { 27 typename vector<Student>::const_iterator i; 28 for(i = vv.begin();i != vv.end();i++) 29 cout<<i->name<<" "<<i->id<<" "<<i->score<<endl; 30 } 31 32 int main() 33 { 34 vector <Student> v; 35 int n,i = 0; 36 string name_,id_; 37 double score_; 38 Student s1; 39 cin>>n; 40 while(1) 41 { 42 cin>>name_>>id_>>score_; 43 v.push_back( Student(name_,id_,score_) ); 44 if(++i == n) 45 break; 46 } 47 cout<<"请输入待查找成绩 :"<<endl; 48 cin>>score_; 49 s1.score = score_; 50 vector <Student> :: iterator p; 51 p = find(v.begin(),v.end(),s1); 52 if(p != v.end()) 53 cout<<"存在成绩 :"<<score_<<endl; 54 else 55 cout<<"不存在该成绩"<<endl; 56 sort(v.begin(),v.end()); 57 Printvector(v); 58 return 0; 59 }
2020-01-11
以上是关于模板的简单使用(vector)的主要内容,如果未能解决你的问题,请参考以下文章