迭代器模式的使用

1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从小到大和从大到小两种次序输出学生信息。

 

 

 java代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Student implements Comparable<Student>{

    private int no;
    private String name;
    private String classid;
    private int age;
    @Override
    public String toString() {
        return "Student [no=" + no + ", name=" + name + ", classid=" + classid + ", age=" + age + "]";
    }
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getClassid() {
        return classid;
    }
    public void setClassid(String classid) {
        this.classid = classid;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public Student() {
        // TODO Auto-generated constructor stub
    }
    
     public int compareTo(Student s) {
            if(this.no >s.no){
                return 1;
            }else if(this.no < s.no){
                return -1;
            }else {
                return 0;
            }
      }
    public static void main(String[]args) throws IOException {
        Student s;
        List<Student> stus=new ArrayList<Student>();
        File file=new File("E:\\JavaEETest\\2-20-iterator-pattern\\src\\stu.txt");
         BufferedReader reader = null;
         reader = new BufferedReader(new FileReader(file));
         String readStr;
         while ((readStr = reader.readLine()) != null) {
             String k[]=readStr.split("\t");
             s=new Student();
             s.setClassid(k[2]);
             s.setAge(Integer.parseInt(k[3]));
             s.setName(k[1]);
             s.setNo(Integer.parseInt(k[0]));
             // System.out.println(k[0]+" "+k[1]+" "+k[2]+" "+k[3]+" "+k.length);
             stus.add(s);
             
         }
            s=new Student();
            stus.add(s);
        
        Collections.sort(stus);
        Iterator<Student> it=stus.iterator();
        while(it.hasNext()){
                System.out.println(it.next());
         }
         reader.close();
    }
    

}

 

 c++代码

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include <iterator>                              // For stream and back insert iterators
#include <algorithm>     
using namespace std;
class Student{

private:
    int no;
    string name;
    string classid;
    int age;

public:
    
    int getNo() {
        return no;
    }
    void setNo(int no) {
        this->no = no;
    }
    string getName() {
        return name;
    }
    void setName(string name) {
        this->name = name;
    }
    string getClassid() {
        return classid;
    }
    void setClassid(string classid) {
        this->classid = classid;
    }
    int getAge() {
        return age;
    }
    void setAge(int age) {
        this->age = age;
    }

    Student() {
        // TODO Auto-generated constructor stub
    }

    bool comp(Student *s, Student *b)
    {
        return s->no<b->no;
    }
    
};
    int main(){
        //Student s;
        vector<Student* > stus;
        ifstream file("stu.txt");
        int  no, age;
        string  y="", z="";
        //while (!file.eof()||file.peek()==EOF){
        while (file >> no >> y >> z >> age){//避免文件末尾读取两次
        Student *s=new Student();
            //file >> no >> y >> z >> age;
            s->setAge(age);
            s->setClassid(z);
            s->setName(y);
            s->setNo(no);
            //cout << no <<" "<< y <<" "<< z <<" "<< age<<endl;
            stus.push_back(s);
            //cout << 1;
        }
        sort(begin(stus), end(stus),
            []( Student *p1, Student *p2) {return p1->getNo() < p2->getNo(); });//从小到大

        for (vector<Student*>::const_iterator iter = stus.begin(); iter != stus.cend(); iter++) {//迭代器进行排序
            cout << (*iter)->getNo() << " "<<(*iter)->getName()<<" " << (*iter)->getClassid()<<" " << (*iter)->getAge()<< endl;
        }
        cout << endl << endl;
        sort(begin(stus), end(stus),
            [](Student *p1, Student *p2) {return p1->getNo() > p2->getNo(); });//从大到小
        for (vector<Student*>::const_iterator iter = stus.begin(); iter != stus.cend(); iter++) {//迭代器进行排序
            cout << (*iter)->getNo() << " " << (*iter)->getName() << " " << (*iter)->getClassid() << " " << (*iter)->getAge() << endl;
        }
        file.close();
        
    }

 

posted @ 2021-11-12 09:46  好吗,好  阅读(45)  评论(0)    收藏  举报