Java/C++实现迭代器模式---学生信息
信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++实现迭代器模式---学生信息的更多相关文章
- 16.java设计模式之迭代器模式
基本需求: 展示一个学校的结构,比如一个学校下面有多个学院,学院下面有多个系,对其节点主要是遍历,与组合模式略有不同 传统方案: 学校<-学院<-系 依次继承 这种方式,在一个页面中展示出 ...
- 简单的了解下Java设计模式:迭代器模式(转载)
迭代器模式定义 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示. Java 开发过程中遍历是常用的.如下边程序: for(int i =0 ; ...
- 折腾Java设计模式之迭代器模式
迭代器模式 Provide a way to access the elements of an aggregate object sequentially without exposing its ...
- java设计模式之迭代器模式
一.迭代器模式简介 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露内部的表示.把游走的任务放在迭代器上,而不是 ...
- jsp和java的结合使用显示学生信息
package com.zyz; public class Student { private String ID; // 学号 private String name; // 姓名 private ...
- 设计模式学习笔记(十六)迭代器模式及其在Java 容器中的应用
迭代器(Iterator)模式,也叫做游标(Cursor)模式.我们知道,在Java 容器中,为了提高容器遍历的方便性,把遍历逻辑从不同类型的集合类中抽取出来,避免向外部暴露集合容器的内部结构. 一. ...
- 设计模式 -- 迭代器模式(Iterator)
--------------------------------------------------------------------- 1.场景问题 考虑这样一个问题: 9个学生对象分别通过数组存 ...
- Java实现功能简单的学生管理系统(附带源代码)
这几天Java学了点新的知识,打算要用这些知识做一个比较简单的管理系统,实战一下子,代码中的功能简洁,可能不多,但是作为一个练手来了解一个项目是怎么样一点一点思考的还是不错的 一.代码中要实现的功能 ...
- 深入理解Java中的迭代器
迭代器模式:就是提供一种方法对一个容器对象中的各个元素进行访问,而又不暴露该对象容器的内部细节. 概述 Java集合框架的集合类,我们有时候称之为容器.容器的种类有很多种,比如ArrayList.Li ...
随机推荐
- 【面经】Java面试突击
基础语法 基本数据结构 Java 的基本数据类型有 8 种,包括 6 种数字类型.1 种字符类型和 1 种布尔类型. 基本数据类型总览 数字类型包括 4 种整数类型和 2 种浮点数类型,4 种整数类型 ...
- k8s命令行web代理神器gotty
目录 介绍 安装 使用示例 -p 指定端口 -c 指定账号密码 -w 支持tty交互 --permit-arguments 支持get参数传参 --random-url 生成随机地址 --reconn ...
- Lua中如何实现类似gdb的断点调试—08支持通过包名称添加断点
在前一篇中我们支持了通过函数名称来添加断点,我们同时也提到了在Lua中一个函数的名称的并不是确定的.准确的说,Lua中的函数并没有名称,所谓名称其实是保存这个函数值的变量的名称. 于是通过函数名称添加 ...
- tensorflow源码解析之framework-shape_inference
目录 什么是形状推断 InferenceContext 关系图 涉及的文件 迭代记录 1. 什么是形状推断 前面我们讲到op的时候,提到了操作的注册器OpRegistry,并且提到,其中注册的数据是一 ...
- 原生 JS 实现 VS Code 自动切换输入法状态!这次没有AHK
上一篇文章:使用 AHK 在 VS Code 中根据上下文自动切换输入法状态 给出一个使用 ahk 在 VSCode 自动切换输入法的方法.不过这个方法实际上很蹩脚,一点都不优(zhuang)雅(bi ...
- vue2版本中slot的基本使用详解
前言 在vue的开发过程中,我们会经常使用到vue的slot插槽组件,vue官方文档的描述: Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案 ...
- springWeb——Servlet
6.1.Servlet简介 servlet是sun公司开发动态web的一门技术 sum在这些API中提供了一个接口叫做:Servlet.开发的两个步骤: 编写一个类,实现Servlet接口 把开发好的 ...
- 从文件下载视角来理解Web API
一.问题源起 从Web From过来的人应该会比较熟悉以下下载文件的代码: [HttpPost] [Route("Download")] public void Download( ...
- 半吊子菜鸟学Web开发4 --Html css学习2
1<a>标签,链接到另一个页面 <a href="目标网址" title="鼠标滑过显示的文本">链接显示的文本</a> H ...
- Mysql之B+树索引实战
索引代价 空间上的代价 一个索引都对应一棵B+树,树中每一个节点都是一个数据页,一个页默认会占用16KB的存储空间,所以一个索引也是会占用磁盘空间的. 时间上的代价 索引是对数据的排序,那么当对表中的 ...