问题描述

实现链表容器模板类,利用模板实现找到公司中工资最高的员工的工资,实现正向反向查找,并且比较两种方法的时间差异。

代码实现

公司类Company.h

#ifndef COMPCONT_H
#define COMPCONT_H#define NUM_EMPLOYEE 10000
#define MAX_SALARY 10000
#include"listTemp.h"
template<class T>
class ListTemp;
class Company
{private:int bestPaid;ListTemp<int> container;public:void inputSalary(); //when input employee from keyboard, store the input data in the container for later use;//please implement the following two methodsvoid findBestPaid(); //go through the container to find the best paid employeevoid findBestPaidReverse(); //go through the container in the reverse order to find the best paid employeevoid printBestPaid() const;
}; #endif

公司类Company.cpp

#include "company.h"
#include <iostream>
#include <cstdlib> // Header file needed to use srand and rand
#include <ctime> // Header file needed to use time
using namespace std;void Company::inputSalary()
{//set the seed for random number generation    srand((unsigned int)time(0));for(int i=0; i < NUM_EMPLOYEE; i++){int rand_number = rand() % MAX_SALARY;container.AddHead(rand_number); // add to the container}
}void Company::findBestPaid()
{bestPaid = 0;ListTemp<int>::Iterator itr = container.Begin();while (!(itr == container.End())){if (*itr >= bestPaid)bestPaid = *itr;itr++;}
}//please implement this
void Company::findBestPaidReverse()
{bestPaid = 0;ListTemp<int>::Iterator itr = container.End();while (!(itr == container.Begin())){itr--;if (*itr >= bestPaid)bestPaid = *itr;//itr--;}}void Company::printBestPaid() const
{cout << "The salary of the best-paid employee is: " << bestPaid << endl;
}// printBestPaid

容器模板类

#ifndef LISTTEMP_H
#define LESTTEMP_H#define NULL 0
template<class T>
class ListTemp
{private:struct Node{T data;Node *next;};Node *head;int size;public:ListTemp();~ListTemp();int getLength() const;bool isEmpty() const;void AddHead(const T newData);//************************declaration of the inner iterator class****************************class Iterator{friend class ListTemp<T>;private:Node *head;Node *curr;Iterator(Node *hear_ptr, Node *curr_ptr); //constructor with Node parameter, defined as privatepublic:Iterator(); //default constructorIterator operator++(int); //post-increment of ++//please implement thisIterator operator--(int); //post-decrement of --T& operator*() const;bool operator==(const Iterator other) const;};//class Iterator//please implement thisIterator Begin() const;//please implement thisIterator End() const;};//************************implementation of the iterator inner class**************************
template<class T>
ListTemp<T>::Iterator::Iterator()
{head = NULL;curr = NULL;
}template<class T>
ListTemp<T>::Iterator::Iterator(Node *head_ptr, Node *curr_ptr)
{head = head_ptr;curr = curr_ptr;
}template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::Iterator::operator++(int)
{Iterator temp = *this;this->curr = curr->next;return temp;
}template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::Iterator::operator--(int)
{Iterator temp = *this;Node *temp_ptr = this->head;while (temp_ptr != NULL && temp_ptr->next != this->curr)temp_ptr = temp_ptr->next;this->curr = temp_ptr;return temp;
}template<class T>
T& ListTemp<T>::Iterator::operator*() const
{return curr->data;
}template<class T>
bool ListTemp<T>::Iterator::operator==(const Iterator other) const
{return curr == other.curr;
}//************************implementation of the Begin and End position**************************
template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::Begin() const
{return Iterator(head, head);
}template<class T>
typename ListTemp<T>::Iterator ListTemp<T>::End() const
{return Iterator(head, NULL);
}//************************implementation of the linked list class template**************************
template<class T>
ListTemp<T>::ListTemp()
{head = NULL;size = 0;
}template<class T>
ListTemp<T>::~ListTemp()
{Node *current = head;Node *temp = NULL;while (current != NULL){temp = current;current = current->next;delete temp;}
}template<class T>
int ListTemp<T>::getLength() const
{return size;
}template<class T>
bool ListTemp<T>::isEmpty() const
{return size == 0;
}template<class T>
void ListTemp<T>::AddHead(const T newData)
{Node *temp = new Node;temp->next = head;temp->data = newData;head = temp;size++;
}#endif

测试main函数

//#include "listTemp.h"
//!!!!千万注意在cpp文件中不能引入模板类.h文件
#include "company.h"
#include <ctime>
#include <iostream>
using namespace std;int main()
{Company cmp;cmp.inputSalary();long start_t, end_t;long start_t2, end_t2;start_t = clock();cmp.findBestPaid();end_t = clock();cmp.printBestPaid();cout << "time cost: " << ((double)(end_t - start_t)) / CLOCKS_PER_SEC << " seconds" << endl;//please measure the time coststart_t2 = clock();cmp.findBestPaidReverse();end_t2 = clock();cmp.printBestPaid();cout << "time cost of reverse: " << ((double)(end_t2 - start_t2)) / CLOCKS_PER_SEC << " seconds" << endl;return 0;
}

实验结果

经验总结

这是一个应用链表容器,实现迭代器细节,利用容器解决问题的很好的例子。初写模板类可能会遇到一些问题,在此做一些经验总结:

1.模板类的定义与实现要在同一个.h文件中完成,不要把定义和实现分开。
2.定义好的模板类需要引入才能够使用。需要特别注意,引入模板类的时候只能在头文件中引入,此处为在Company.h中引入,切忌不能在任何cpp文件中再加入listTemp.h文件,这样编译器会不明确,会出现

类似这样的错误。

3.关于模板类的


问题,在模板类在声明这些是为了防止命名冲突,需要注意前后的匹配#ifndef,#define与#endif需要同时出现。

3.在实现迭代器的++,–操作时,需要明确–为前置还是后置,准确区分前置与后置。

c++模板类(链表),实现正向反向找到链表中最大值,并比较时间差异相关推荐

  1. java 查找链表中间元素,如何找到链表的中间节点?

    1. 碎碎念 遥想后端君当年,曾经也是学校ACM队的一员,但参加过级别最高的比赛,同时也是ACM方面获得的最大成就,不过是天梯赛三等奖(当时天梯赛在浙江还只是省B级别的,现在已经算国赛了),犹记得当时 ...

  2. C++知识点61——typename与class、模板编程与继承、模板类和友元、类模板与static成员

    一.typename与class的异同 1.啥时候既可以使用typename,又可以使用class? 当表示模板参数的时候,二者没有区别 2.啥时候只能使用typename,不能使用class? 当模 ...

  3. 自定义模板类(循环队列)

    自定义模板类--循环链表 正做的这个链表的时候,遇到了,对于友元函数的处理问题 实现代码如下(配有测试main): #include <iostream> using namespace ...

  4. Google Test(GTest)使用方法和源码解析——模板类测试技术分析和应用

    写C++难免会遇到模板问题,如果要针对一个模板类进行测试,似乎之前博文中介绍的方式只能傻乎乎的一个一个特化类型后再进行测试.其实GTest提供了两种测试模板类的方法,本文我们将介绍方法的使用,并分析其 ...

  5. 三维重建16:概率图模型 模板类编程

    刚刷了一部分网络题,又出了个模板类编程.没人能从面试中得到自己想要的方法,只能得到能看得到的结果!!! 一 概率图模型 贝叶斯模型,真是推导不出来了!贝叶斯函数貌似也写不出来了! 参考:斯坦福概率图模 ...

  6. 模板类出现外部符号无法解析错误

    问题:如果将类模板的声明和实现写在两个独立的文件中,会出现"error LNK2019: 无法解析的外部符号 "的错误 解决方法:(1)在头文件末尾include源文件:LNK20 ...

  7. C++中模板类的静态成员

    目录 C++中模板类的静态成员 为什么需要模板类的静态成员? 代码示例 C++中模板类的静态成员 为什么需要模板类的静态成员? 模板类的静态成员和普通类,普通函数的静态成员一样,我们想在函数调用后留些 ...

  8. C++模板类声明和定义几种写法

    为什么模板类的实现放在cpp会出错 在编译用到了模板类的编译单元时,编译器需要访问方法或者类的实现,以实例化它们. 如果这些实现不在头文件中,则它们将不可访问,因此编译器将无法实例化模板,进而会导致编 ...

  9. 链表操作---面向过程--到---面型对象---到模板类

    设计一个链表操作,从分设计到实现分别从3个step进行  (1)面向过程的程序设计---结构体+函数 /* 链表操作-----step1-----用结构体实现链表操作链表设计----需求分析 1.创建 ...

最新文章

  1. stm32f302实现斩波控制步进电机_什么是步进电机控制器?
  2. Safari回传值给应用程序
  3. 《算法》练习题1.1.1--1.1.39 答案解析
  4. SAP CRM get_children 方法里面参数 iv_as_copy 有什么用?
  5. 使用OpenCV,Keras和Tensorflow构建Covid19掩模检测器
  6. 训练不出结果_训练赛惨败SKT?FPX直播透露拿冠军原因!Karsa再谈离开RNG?
  7. bash 别名_Linux的10个方便的Bash别名
  8. python调用edge_Abaqus中Python通过findAt方法建立region区域
  9. 毕业后,她用1年时间拿下了30W年薪的阿里数据分析岗
  10. (1)初识云计算-《云计算核心技术剖析》学习笔记
  11. 向日葵和teamviewer免费版的替代品RD远控。
  12. 修改 exchange服务器,升级Exchange2010-新服务器更改IP
  13. 将VSCode添加到鼠标右键菜单
  14. CAD导入图片怎么操作?简单几步就导入
  15. 7款家用智能摄像头横评:小米、乐橙、TP-LINK、海康威视、360、智汀、华为
  16. android_基础_修改系统背景(状态栏颜色、导航栏颜色、标题栏颜色等等)
  17. c语言;文件名批量修改要求可以修改整个文件内文件名或后缀名自动,Python批量修改文件名...
  18. Echarts实现省级到市级地图下钻
  19. 2022第三届全国大学生网络安全精英赛练习题(5)
  20. hust_os_shell算命大师

热门文章

  1. 企业行政6s管理制度概念及具体规定
  2. 自定义整型转字符串函数
  3. 遗传算法(四)MATLAB GA工具箱使用 附解TSP问题
  4. 安装显卡GTX1080Ti显卡在Ubuntu16.04 安装教程
  5. 浏览器前端JS批量下载文件
  6. ARM嵌入式裸机简单使用
  7. 泰语字库,泰文字库,泰文组合算法,泰语组合算法
  8. Windows 中GDI、设备描述表和位图
  9. 皮肤晒伤了怎么办(小朋友皮肤晒伤了怎么办)
  10. 数据共享和数据开放如何改变世界论文_GW-ICC2019丨数据共享,开启临床研究新模式...