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

Java代码:

public class Student implements Comparable<Student>{
private int studentid;
private String name;
private int age;
private String major; public Student(int studentid, String name, int age, String major) {
super();
this.studentid = studentid;
this.name = name;
this.age = age;
this.major = major;
} // 三个返回结果都要写出来
public int compareTo(Student o) {
if(this.studentid > o.studentid){
return -1;
}else if(this.studentid < o.studentid){
return 1;
}else {
return 0;
}
} @Override
public String toString(){
return "姓名: " + this.name + ". 学号: " + this.studentid + ". 年龄: " + this.age
+ ". 专业: " + this.major;
}
} public class Student2 implements Comparable<Student2>{
private int studentid;
private String name;
private int age;
private String major; public Student2(int studentid, String name, int age, String major) {
super();
this.studentid = studentid;
this.name = name;
this.age = age;
this.major = major;
} // 三个返回结果都要写出来
public int compareTo(Student2 o) {
if(this.studentid < o.studentid){
return -1;
}else if(this.studentid > o.studentid){
return 1;
}else {
return 0;
}
} @Override
public String toString(){
return "姓名: " + this.name + ". 学号: " + this.studentid + ". 年龄: " + this.age
+ ". 专业: " + this.major;
}
} public class Client {
public static void main(String[] args) {
Student s1 = new Student(20193250, "张雨轩", 19, "软件工程专业");
Student s2 = new Student(20193999, "李四", 30, "材料专业");
Student s3 = new Student(20196654, "王五", 29, "机械专业");
Student s4 = new Student(20193367, "赵六", 34, "工商管理专业");
Student s5 = new Student(20193396, "张三", 34, "土木专业");
Student s6 = new Student(20193396, "孙七", 34, "电气专业");
Student2 s7 = new Student2(20193250, "张雨轩", 19, "软件工程专业");
Student2 s8 = new Student2(20193999, "李四", 30, "材料专业");
Student2 s9 = new Student2(20196654, "王五", 29, "机械专业");
Student2 s10 = new Student2(20193367, "赵六", 34, "工商管理专业");
Student2 s11 = new Student2(20193396, "张三", 34, "土木专业");
Student2 s12 = new Student2(20193396, "孙七", 34, "电气专业");
List<Student> list = new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
Collections.sort(list);
System.out.println("按照学号从大到小输出: ");
for(Student stu : list){
System.out.println(stu.toString());
}
System.out.println("-----------------------------------------------------------------");
List<Student2> list2 = new ArrayList<Student2>();
list2.add(s7);
list2.add(s8);
list2.add(s9);
list2.add(s10);
list2.add(s11);
list2.add(s12);
Collections.sort(list2);
System.out.println("按照学号从小到大输出: ");
for(Student2 stu : list2){
System.out.println(stu.toString());
}
}
}

C++代码:

#include<iostream>
#include <vector>
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<<". 学号: "<<this->studentid <<". 年龄: "<< this->age<< ". 专业: " << 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(20193250, "张雨轩", 19, "软件工程专业");
Student *s2 = new Student(20193999, "李四", 30, "材料专业");
Student *s3 = new Student(20196654, "王五", 29, "机械专业");
Student *s4 = new Student(20193367, "赵六", 34, "工商管理专业");
Student *s5 = new Student(20193396, "张三", 34, "土木专业");
Student *s6 = new Student(20193396, "孙七", 34, "电气专业");
vector<Student*> vec;
vec.push_back(s1);
vec.push_back(s2);
vec.push_back(s3);
vec.push_back(s4);
vec.push_back(s5);
vec.push_back(s6);
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();
}
}

运行结果:

Java/C++实现迭代器模式---学生信息的更多相关文章

  1. 16.java设计模式之迭代器模式

    基本需求: 展示一个学校的结构,比如一个学校下面有多个学院,学院下面有多个系,对其节点主要是遍历,与组合模式略有不同 传统方案: 学校<-学院<-系 依次继承 这种方式,在一个页面中展示出 ...

  2. 简单的了解下Java设计模式:迭代器模式(转载)

    迭代器模式定义 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示. Java 开发过程中遍历是常用的.如下边程序: for(int i =0 ; ...

  3. 折腾Java设计模式之迭代器模式

    迭代器模式 Provide a way to access the elements of an aggregate object sequentially without exposing its ...

  4. java设计模式之迭代器模式

    一.迭代器模式简介 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露内部的表示.把游走的任务放在迭代器上,而不是 ...

  5. jsp和java的结合使用显示学生信息

    package com.zyz; public class Student { private String ID; // 学号 private String name; // 姓名 private ...

  6. 设计模式学习笔记(十六)迭代器模式及其在Java 容器中的应用

    迭代器(Iterator)模式,也叫做游标(Cursor)模式.我们知道,在Java 容器中,为了提高容器遍历的方便性,把遍历逻辑从不同类型的集合类中抽取出来,避免向外部暴露集合容器的内部结构. 一. ...

  7. 设计模式 -- 迭代器模式(Iterator)

    --------------------------------------------------------------------- 1.场景问题 考虑这样一个问题: 9个学生对象分别通过数组存 ...

  8. Java实现功能简单的学生管理系统(附带源代码)

    这几天Java学了点新的知识,打算要用这些知识做一个比较简单的管理系统,实战一下子,代码中的功能简洁,可能不多,但是作为一个练手来了解一个项目是怎么样一点一点思考的还是不错的 一.代码中要实现的功能 ...

  9. 深入理解Java中的迭代器

    迭代器模式:就是提供一种方法对一个容器对象中的各个元素进行访问,而又不暴露该对象容器的内部细节. 概述 Java集合框架的集合类,我们有时候称之为容器.容器的种类有很多种,比如ArrayList.Li ...

随机推荐

  1. LeetCode-038-外观数列

    外观数列 题目描述:给定一个正整数 n ,输出外观数列的第 n 项. 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述. 你可以将其视作是由递归公式定义的数字字符串序列: ...

  2. LeetCode-033-搜索旋转排序数组

    搜索旋转排序数组 题目描述:整数数组 nums 按升序排列,数组中的值 互不相同 . 在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行 ...

  3. netty搭建rpc框架

    介绍 netty想必大家都不陌生,我就不废话介绍了...(主要是懒,网上资料很多的) 本文主要使用netty搭建rpc远程调用框架,实现了个注册中心微服务,整合了springboot例子... 开发内 ...

  4. w10环境vs2017,vs2019配置Opengl快捷方法

    最近,计算机图形学老师向我们布置了任务,配置自己的opengl.百度之后我发现很多教程和方法尝试之后,我发现一种简单的方法来分享给大家. 首先我的软件配置是w10专业版系统+Visual Stdio ...

  5. 新建SpringBoot项目报错

    新建一个Springboot项目时,当勾选了SQL相关的依赖(如引入了jpa 或MyBatis依赖),直接启动项目时报错 原因:没有配置数据库相关的属性,如 url driver 等 解决办法:在ap ...

  6. Mybatis中的基本对象的生命周期和作用域

    不同作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题 SqlSessionFactoryBuilder 这个类可以被实例化.使用和丢弃,一旦创建了 SqlSessionFact ...

  7. redis哨兵功能

    redis哨兵功能 redis-Sentinel(哨兵) 前言 当用redis作master-slave的高可用时,如果master本身宕机,redis本身或者客户都没有实现主从切换的功能 redis ...

  8. 『现学现忘』Docker基础 — 32、通过DockerFile的方式挂载数据卷

    目录 1.简单了解一下DockerFile 2.通过DockerFile的方式挂载数据卷 (1)创建DockerFile文件 (2)编辑Dockerfile文件 (3)构建Dokcer镜像 (4)启动 ...

  9. 手撕代码:leetcode 309最佳买卖股票时机含冷冻期

    转载于:https://segmentfault.com/a/1190000014746613 给定一个整数数组,其中第i个元素代表了第i天的股票价格. 设计一个算法计算出最大利润.在满足以下约束条件 ...

  10. Hystrix相关注解?

    @EnableHystrix:开启熔断 @HystrixCommand(fallbackMethod="XXX"):声明一个失败回滚处理函数XXX,当被注解的方法执行超时(默认是 ...