数据结构+C++_№2
来源:互联网 发布:js 端获取 服务器地址 编辑:程序博客网 时间:2024/06/05 18:27
模板
.mycode { font:bold 15 Times; color:#330033; background-color:#f7eef7; border-width:2px; border-style:dashed; border-color:pink; padding:0.5em; FILTER: Alpha(Opacity=50); } 今天看了模板一章,真是头疼啊,什么乱七八糟的东西,template,class,以是<>的,真是一个字乱,看书上的源代码,还有点感觉,不过有些怪,上机调试一下,果不其然,错误十好几个:|,真是有些悲哀啊,书都出了这么多年了,错误还是错误,根本没有改,发行量小一些还好,偏偏发行量还那么大,如果一点也不了解的话,死的真的会很“抽象”的(呵呵,幸亏花了100多元搞了本C++ Primer 3rd,要不然,偶也被抽象了,汗一个先)
下面是代码,已经改过很多了,可以运行了,不过,就是不能把那两个成员:int ArraySize,Type* Element设置成private,否则那个重载>>老是报错,InStream>>InList.ArraySize中的ArraySize因为是private,无法访问,试了很多,也没解决,不知道怎么回事,看C++ Primer 3rd上也有类似代码,也是那样的,不知道是什么原因,愚蒙中。。。。
还有就是用VC++ 6.0明明好好的,到了.NET里老是出错,说什么“包含头文件的非预料结尾”,文件明明是好的,真是的也有这种错误。
/* 数据结构(用面向对象方法与C++描述)
* 第20页1.6模板(templates)
* 示例算法之类定义文件:datalist.h
* -------by Speed1
*/
#ifndef DATALIST_H //类定义放在头文件datalist.h中
#define DATALIST_H
#include <iostream.h>
//const int SIZE=10;
template <class Type> //模块标识
class dataList //类标识
{
private:
void Swap(const int mark1,const int mark2);
int MaxKey(const int low,const int high);
public:
int ArraySize;
Type* Element; //存入数据的数组dataList(int size=10 ): ArraySize(size), Element(new Type[size]) {} //构造函数
~dataList() {delete []Element;} //析构函数
void Sort();
friend ostream& operator<<(ostream& outStream,const dataList<Type>& outList);
friend istream& operator>>(istream& inStream,const dataList<Type>& inList);
};
#endif
/* 数据结构(用面向对象方法与C++描述)
* 第20页1.6模板(templates)
* 示例算法之类的操作实现头文件selecttm.h
* -------by Speed1
*/
#ifndef SELECTTM_H //类服务的实现入在头文件selecttm.h中
#define SELECTTM_H
#include "datalist.h"
template<class Type> void dataList<Type>::Swap(const int mark1,const int mark2)
{
//交换由mark1,mark2为下标的两个数组元素的值。
Type temp=Element[mark1];
Element[mark1]=Element[mark2];
Element[mark2]=temp;
}
template <class Type> int dataList<Type>::MaxKey(const int low,const int high)
{
//查找数组Element[low]~Element[high]中的最大值,函数返回其位置
int max=low; //假定第low个为最大者,由max标识之
for(int k=low+1;k<=high;k++)
if(Element[max]<Element[k]) max=k;
return max;
}
template <class Type> ostream& operator<<(ostream& OutStream,const dataList<Type>& OutList)
{
//输出重载操作符<<的实现,输出对象为OutList,输出对象为OutStream。
OutStream<<"Array Contents:/n";
for(int i=0;i<OutList.ArraySize;i++)
OutStream<<OutList.Element[i]<<" ";
OutStream<<endl;
OutStream<<"Array Current Size:"<<OutList.ArraySize<<endl;
return OutStream;
}
template <class Type> istream& operator>>(istream& InStream,dataList<Type>& InList)
{
//输入操作符重载>>的实现,输入对象为InList,输入流对象为:InStream。
cout<<"Enter array Current Size:";
InStream>>InList.ArraySize;
cout<<"Enter array element:/n";
for(int i=0;i<InList.ArraySize;i++)
{
cout<<"Element"<<i<<":";
InStream>>InList.Element[i];
}
return InStream;
}
template <class Type> void dataList<Type>::Sort()
{
//按非递减的顺序对ArraySize个关键码Element[0]~Element[ArraySize-1]排序
for(int i=ArraySize-1;i>0;i--)
{
int j=MaxKey(0,i);
if(j!=i) Swap(j,i);
}
}
#endif
/* 数据结构(用面向对象方法与C++描述)
* 第20页1.6模板(templates)
* 示例算法之主程序
* -------by Speed1
*/
#include "selecttm.h"
const int SIZE=10;
int main()
{
dataList <int> TestList(SIZE);
cin>>TestList;
cout<<"Testing Selection Sort;/n"<<TestList<<endl;
TestList.Sort();
cout<<"After sorting:/n"<<TestList<<endl;
return 0;
}
- 数据结构+C++_№2
- 数据结构+C++_№1
- 数据结构+C++_№3
- 数据结构+C++_№4
- 数据结构+C++_№5
- c语言_数据结构_顺序表
- c语言_数据结构_单链表
- 【C语言】树_数据结构_练习
- 【C语言】图_数据结构_练习
- 【C语言】查找_数据结构_练习
- C数据结构算法_搜索
- 数据结构+C++_№5_顺序表错误更正及新的代码
- 数据结构_复习2
- 数据结构(C#)_排序算法(冒泡排序)
- 数据结构(C#)_排序算法(插入排序)
- 数据结构(C#)_排序算法(插入排序)
- 数据结构(C#)_排序算法(归并排序)
- 数据结构(C#)_查找(静态查找)
- (原创) 关于PB开发人员酬薪现状的讨论
- 硬盘坏道修复全攻略
- BIOS的设置.注册表的修改与电脑故障的排除
- 中国城市望穿秋水
- 想做一个hibernate 3的企业开发专题
- 数据结构+C++_№2
- 富人和穷人对待12大问题的经典差异(转贴)
- Delphi开发编码规范
- hiberfil.sys
- 释义
- develop mophun programme
- 电脑的备份与病毒防护
- Pascal精要笔记
- 星球学院(1)