迭代器模式

package Iterator;

public class Student {
    private String name;
    private long id;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Student(){

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

package Iterator;


import com.sun.xml.internal.ws.addressing.WsaTubeHelper;

import java.util.*;

public class Client {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student("lisi",20194001,20));
        list.add(new Student("wangwu",20194002,21));
        list.add(new Student("zhaosi",20194003,22));
        list.add(new Student("xiaoming",20194005,20));
        list.add(new Student("xiaohong",20194004,22));
        list.add(new Student("xiaohuang",20194010,20));
        list.add(new Student("xiaolan",20194008,20));
        System.out.printf("正序排列:\n");
        Collections.sort(list,new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                // 返回值为int类型,大于0表示正序,小于0表示逆序
                
return (int)(o1.getId()-o2.getId());
            }
        });
        Iterator<Student> listIt = list.iterator();
        while(listIt.hasNext()){
            Student stu=listIt.next();
            System.out.println("姓名:"+stu.getName()+" 学号:"+stu.getId()+" 年龄:"+stu.getAge());
        }
        System.out.printf("倒顺排列:\n");
        Collections.sort(list,new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                // 返回值为int类型,大于0表示正序,小于0表示逆序
                
return (int)(o2.getId()-o1.getId());
            }
        });
        Iterator<Student> listIt2 = list.iterator();
        while(listIt2.hasNext()){
            Student stu=listIt2.next();
            System.out.println("姓名:"+stu.getName()+" 学号:"+stu.getId()+" 年龄:"+stu.getAge());
        }
    }
}

 

 

C++:

 

// sejimoshi.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

//

 

#include <iostream>

#include <list>

#include<algorithm>

using namespace std;

 

class Student

{

private:

    string name;

    long id;

    int age;

public:

    string getName() {

        return name;

    }

    long getId() {

        return id;

    }

    int getAge() {

        return age;

    }

    void setName(string name) {

        this->name = name;

    }

    void setAge(int age) {

        this->age = age;

    }

    void setId(long id) {

        this->id = id;

    }

    Student(string name, long id, int age) {

        this->name = name;

        this->id = id;

        this->age = age;

    }

};

 

 

//比较函数:根据结构体里面的整型number排序

bool sortStuInt(Student m1, Student m2)

{

    return m1.getId() < m2.getId();

}

//比较函数:根据结构体里面的字符串name排序

bool comStuString(Student m1, Student m2)

{

    if (m1.getId() > m2.getId())

    {

        return true;

    }

    else

    {

        return false;

    }

}

 

int main()

{

    std::cout << "Hello World!\n";

    list<Student> list_student;

    list_student.push_back(Student("lisi", 20194001, 20));

    list_student.push_back(Student("wangwu", 20194002, 21));

    list_student.push_back(Student("zhaosi", 20194003, 22));

    list_student.push_back(Student("xiaoming", 20194005, 20));

    list_student.push_back(Student("xiaohong", 20194004, 22));

    list_student.push_back(Student("xiaohuang", 20194010, 20));

    list_student.push_back(Student("xiaolan", 20194008, 20));

    cout << "迭代器正序遍历\n";

    list_student.sort(sortStuInt);

    //使用begin()/end()迭代器函数对输出list容器中的元素

    for (std::list<Student>::iterator it = list_student.begin(); it != list_student.end(); ++it) {

        std::cout << (*it).getId()<<endl;

    }

    cout << "迭代器倒序遍历\n";

    list_student.sort(comStuString);

    //使用begin()/end()迭代器函数对输出list容器中的元素

    for (std::list<Student>::iterator it = list_student.begin(); it != list_student.end(); ++it) {

        std::cout << (*it).getId() << endl;

    }

}

 

bool cmp_1(Student x, Student y) {

    return x.getId() > y.getId();

}

bool cmp_2(Student x, Student y) {

    return x.getId() < y.getId();

}

posted @ 2021-11-11 21:20  xjspyx  阅读(22)  评论(0编辑  收藏  举报