10.10

实验18:迭代器模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 

1、理解迭代器模式的动机,掌握该模式的结构;

2、能够利用迭代器模式解决实际问题。

 

[实验任务一]JAVAC++常见数据结构迭代器的使用

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

实验要求:

1. 搜集并掌握JAVAC++中常见的数据结构和迭代器的使用方法,例如,vector, list, mapset等;

2. 提交源代码;

3. 注意编程规范。

 

复制代码
package rjsj.no18;
import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;
class Student implements Comparable<Student>{

    private String name;
    private int sid;
    private int age;

    public Student(String name, int sid, int age) {
        this.name = name;
        this.sid = sid;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "姓名='" + name + '\'' +
                ", 学号=" + sid +
                ", 年龄=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        if (this.sid > o.sid){
            return -1;
        } else if (this.sid < o.sid){
            return 1;
        } else {
            return 0;
        }
    }
}class IteratorDemo {
    public static void main(String[] args) {
        Student *s1 = new Student(20223749, "刘子枫", 21, "软工");
    Student *s2 = new Student(20223748, "李四", 21, "经管");
    Student *s3 = new Student(20223747, "王五", 22, "软工");
    Student *s4 = new Student(20223745, "赵六", 20, "机械");
        List<Student> list = new ArrayList<Student>();
        list.add(student1);list.add(student2);list.add(student3);
        list.add(student4);list.add(student5);
        Collections.sort(list);
        System.out.println("按学号排序输出:");
        Iterator i = list.iterator();
        while (i.hasNext()){
            System.out.println(i.next().toString());
        }
    }
}


c++
#include<iostream>
#include <vector>
#include<algorithm>using namespace std;class Student{public:
     long studentid;
     string name;
     int age;
     string major;public:
     Student(long studentid, string name, int age, string major) {
        this->studentid = studentid;
        this->name = name;
        this->age = age;
        this->major = major;
    }
    void show(){
        cout<<"姓名: "<<this->name<<"\t学号: "<<this->studentid <<"\t年龄: "<< this->age<< "\t专业: " << this->major<<endl;
    }
};bool compMax(Student *a,Student *b){
    if (a->studentid> b->studentid)
         return true;
     else
         return false;
}bool compMin(Student *a,Student *b){
    if (a->studentid< b->studentid)
         return true;
     else
         return false;
}int main(){
    Student *s1 = new Student(20223749, "刘子枫", 21, "软工");
    Student *s2 = new Student(20223748, "李四", 21, "经管");
    Student *s3 = new Student(20223747, "王五", 22, "软工");
    Student *s4 = new Student(20223745, "赵六", 20, "机械");
    vector<Student*> vec;
    vec.push_back(s1);
    vec.push_back(s2);
    vec.push_back(s3);
    vec.push_back(s4);
    cout<<"按照学号从大到小输出: "<<endl;
    vector<Student*>::iterator it;
    sort(vec.begin(), vec.end(),compMax);
    for(it=vec.begin();it!=vec.end();it++){
        (*it)->show();
    }
    cout<<"-----------------------------------------------------------------"<<endl;
    cout<<"按照学号从小到大输出: "<<endl;
    sort(vec.begin(), vec.end(),compMin);
    for(it=vec.begin();it!=vec.end();it++){
        (*it)->show();
    }
}
复制代码

 

posted @   奶油冰激凌  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示