#include<iostream.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; template<class type> type add(type a,type b) { return a+b; } template<class type,int size> class Array { private: type* p; public: Array() { p = new type[size]; } type GetAt(int i) { return *(this->p + i); } void SetAt(int i,type val) { *(this->p + i) = val; } ~Array() { if(p!=NULL) delete[] p; } }; class student { public: int sid; char name[10]; int age; public: student() { } student(int sid,char* pname,int age) { this->sid=sid; strcpy(this->name,pname); this->age=age; } void print() { cout<<this->sid<<" "<<this->name<<" "<<this->age<<endl; } }; bool compare(student& st1,student& st2) { if(st1.age>st2.age) return true; else return false; } void main() { vector<student> vec; student st1(1,"aa",24); student st2(2,"bb",21); student st3(3,"cc",20); student st4(4,"dd",19); vec.push_back(st1); vec.push_back(st2); vec.push_back(st3); vector<student>::iterator it; it = vec.begin(); it++; vec.insert(it,st4); for(it = vec.begin();it!=vec.end();it++) { (*it).print(); } cout<<endl<<endl; sort(vec.begin(),vec.end(),compare); for(it = vec.begin();it!=vec.end();it++) { (*it).print(); } } #include<iostream.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; template<class type> type add(type a,type b) { return a+b; } template<class type,int size > class Array { private: type* p; public: Array() { p = new type[size]; } type GetAt(int i) { return *(this->p + i); } void SetAt(int i,type val) { *(this->p + i) = val; } ~Array() { if(p!=NULL) delete[] p; } }; class student { public: int sid; char name[10]; int age; public: student() { } student(int sid,char* pname,int age) { this->sid = sid; strcpy(this->name,pname); this->age = age; } /* student(student& st) { this->sid = st.sid; strcpy(this->name,st.name); this->age = st.age; }*/ void print() { cout<<this->sid<<" "<<this->name<<" "<<this->age<<endl; } }; bool compare(student& st1,student& st2) { if(st1.age>st2.age) return true; else return false; } void main() { vector<student> vec; student st1(1,"aa",34); student st2(2,"bb",23); student st3(3,"cc",34); student st4(4,"dd",45); vec.push_back(st1); vec.push_back(st2); vec.push_back(st3); /* for(int i=0;i<vec.size();i++) { vec[i].print(); } */ vector<student>::iterator it; it = vec.begin(); it++; vec.insert(it,st4); for(it = vec.begin();it!=vec.end();it++) { (*it).print(); } cout<<endl<<endl; sort(vec.begin(),vec.end(),compare); for(it = vec.begin();it!=vec.end();it++) { (*it).print(); } }