每日博客2

软件设计                  石家庄铁道大学信息学院

 

实验18:迭代器模式

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

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

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

 

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

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

实验要求:

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

2. 提交源代码;

Java:

package test18;

 

import java.util.*;

 

public class Client {

 

    public static void maxSort(Collection c){

        Iterator iterator = c.iterator();

 

        while (iterator.hasNext()){

            System.out.println(iterator.next().toString());

        }

    }

 

    public static void main(String[] args) {

        List<Students> students = new ArrayList<>();

 

        students.add(new Students("hzy",123,"12"));

        students.add(new Students("hzy",13,"12"));

        students.add(new Students("hzy",156,"12"));

        students.add(new Students("hzy",78,"12"));

 

        //正序输出

        System.out.println("=============正序输出================");

        Collections.sort(students);

        Client.maxSort(students);

        //倒序输出

        System.out.println("=============倒序输出================");

        Collections.reverse(students);

        Client.maxSort(students);

    }

 

}

package test18;

 

import test07.StudentNo;

 

public class Students implements Comparable<Object>{

 

    private String name;

    private int no;

    private String age;

 

    public void setName(String name) {

        this.name = name;

    }

 

    public void setAge(String age) {

        this.age = age;

    }

 

    public void setNo(int no) {

        this.no = no;

    }

 

    public String getName() {

        return name;

    }

 

    public String getAge() {

        return age;

    }

 

    public int getNo() {

        return no;

    }

 

    public Students(String name,int no,String age){

        this.name = name;

        this.no = no;

        this.age = age;

    }

 

    @Override

    public String toString() {

        return "Students{" +

                "name='" + name + '\'' +

                ", no='" + no + '\'' +

                ", age='" + age + '\'' +

                '}';

    }

 

    /**

     * 实现Comparable<Object>接口,重写排序方法

     * @param o

     * @return

     */

    @Override

    public int compareTo(Object o) {

        Students o1 = (Students)o;

 

        return o1.no-this.no;

    }

}

C++:

#include <iostream>

#include <vector>

using namespace std;

 

class Students{

private:

    string name;

    string no;

    string age;

public:

    Students(string name,string no,string age){ this->name=name;

        this->no=no;

        this->age=age;}

    void setName(string name){this->name = name;}

    void setNo(string no){ this->no=no;}

    void setAge(string age){ this->age=age;}

    string getName(){return this->name;}

    string getNo(){return this->no;}

    string getAge(){return this->age;}

    string toString() {

        return "Students{name=" + name + '\'' +

               ", no='" + no + '\'' +

               ", age='" + age + '\'' +

               '}';

    }

};

 

int main()

{

    vector<Students> v;  //v是存放int类型变量的可变长数组,开始时没有元素

    Students student1("hzy","123","12");

    Students student2("hzy","124","12");

    Students student3("hzy","125","12");

    Students student4("hzy","126","12");

    Students student5("hzy","127","12");

    v.push_back(student1);

    v.push_back(student2);

    v.push_back(student3);

    v.push_back(student4);

    v.push_back(student5);

    cout<<"使用正向迭代器"<<endl;

    vector<Students>::iterator i;  //定义正向迭代器

    for (i = v.begin(); i != v.end(); ++i) {  //用迭代器遍历容器

        cout << i->toString() << endl;  //*i 就是迭代器i指向的元素

    }

    cout<<"使用反向迭代器"<<endl;

    //用反向迭代器遍历容器

    for (vector<Students>::reverse_iterator j = v.rbegin(); j != v.rend(); ++j)

        cout << j->toString() << endl;

    return 0;

}

 

 

3. 注意编程规范。

 

 

posted @ 2023-11-20 22:48  一个不会起名字的人  阅读(4)  评论(0编辑  收藏  举报