18.迭代器模式

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

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

代码

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;
}
posted @ 2022-11-05 21:48  又一岁荣枯  阅读(29)  评论(0编辑  收藏  举报