C++ 模板的一个实践

 /************************************
          WZ ASUST   2016
  模板写了一下,但发现不是模板
************************************/
#include <iostream>
 using namespace std;
#define OK 1
 #define ERROR 0
 template <class T>
 class Linklist
{
 public:
 Linklist(int len=10,int max=20)
  :length(len)
   ,maxsize(max)
   {
    elem = new T[maxsize];
    cout<<"linklist"<<endl;
    cout<<elem<<endl;
    cout<<elem+20<<endl;
    *(elem+20)=1;
    cout<<*(elem+20)<<endl;
   }
 int Getlength(Linklist < T > & L);
 int Getmaxsize(Linklist < T > & L);
 int DestroyList(Linklist < T > & L);
 ~Linklist() //销毁函数
 {
  if(length==0);delete elem;
  cout<<"DestroyList"<<endl;
 }; 
 int ClearList(Linklist < T > & L);
 void ShowList(Linklist < T > & L);
 void GetElem( int i , T  e);

/*
bool ReturnElem( int i , T &e)//获得第i个元素,e必须是T类型的,例如int e
   {
    if(i<1||i>length)
             return false;
     if(    e=elem[i]) 
             return true;
     else 
             return  false;
   }
*/
 T LocateElem(Linklist < T > & L , T e);//找到e元素的位置
 T InsertElem(Linklist < T > & L , int i , T e);//插入
 T paixu();
 T reverse();
 private:
 int length;
 int maxsize;
 int *elem;
};

template<class T>
 int Linklist<T>::Getlength( Linklist &L)
 {
  cout<<L.length<<endl;
 }
template<class T>
 int Linklist<T>::Getmaxsize( Linklist &L)
 {
  cout<<"maxsize is :"<<L.maxsize<<endl;
 }
template<class T>
 int Linklist<T>::DestroyList(Linklist <T> &L) //销毁顺式表
 {
  L.~Linklist();
 }
template <class T>
 int Linklist<T>::ClearList(Linklist < T > &L) //清空顺式表
 {
  L.length = 0;
  if(L.length == 0)
  {
   cout<<"Clear OK"<<endl;
   return 1;
  }
  else
  {
   cout<<"Clear ERROR"<<endl;
   return 0;  
  }
 }
template <class T>
 void Linklist <T>::ShowList(Linklist <T> &L)
 {
  for(int i = 0 ; i<length ; i ++)
   cout<<L.elem[i]<<" ";
  cout<<endl;
 }
 
template <class T>
 void Linklist <T>::GetElem( int i ,T  e)
 {//just test
  *(elem+i)=e; 
  length++;
 }
template <class T>
 T Linklist <T>::LocateElem (Linklist <T> &L , T e)
 {
  for(int i = 0 ; i < L.length ; i ++)
  {
   if( L.elem[i] == e)
   {
    return i;
    break;
   }
  }
  return -1;
 }
template <class T>
 T Linklist<T>::InsertElem(Linklist <T> &L , int i , T   e)
 {
  if(i<1||i>L.length)return ERROR;
  for(int j = L.length ; j >i ; j--)
  {
   L.elem[j] = L.elem[j-1];
  }
  L.elem[i-1] = e;
  L.length ++;
  return 1;
 }
template <class T>
 T Linklist<T>::paixu()
 {
  int i=0,j=0,state=1;
  T t=0;
  for(i=0;i<length&&state;i++)
  { 
   for(j=length;j>i;j--)
    if(elem[j]<elem[j-1])
   {t=elem[j];elem[j]=elem[j-1];elem[j-1]=t;}
  }
 }
template <class T>
 T Linklist<T>::reverse()
 {
  int i=0,j=length;
  T t=0;
  for(;i<j;i++,j--)
  {
   t= elem[i];
   elem[i]=elem[j];
   elem[j]=t;
  }
 }
int main()
{
 int e=10;
 Linklist<int> L;
 //L.initLinklist(L,20);
 L.ShowList(L);
 cout<<"WZZX"<<endl<<L.Getlength(L)<<"WZZX"<<endl;
 L.InsertElem(L,1,3);
 L.GetElem(1,3);
 L.ShowList(L);
 L.InsertElem(L,5,7);
 L.GetElem(6,8);
 L.ShowList(L);
 /// L.ReturnElem(1,&e); //error
 cout<<"----------------"<<endl;   
 cout<<"1:"<<L.LocateElem(L,3)<<endl; //bug  just once
 cout<<"e="<<e<<endl;
 cout<<"----------------"<<endl;   
 cout<<"2:reverse"<<L.reverse()<<endl;  
 L.ShowList(L);
 cout<<"----------------"<<endl;   
 cout<<"3:paixu"<<L.paixu()<<endl;  
 L.ShowList(L);

 cout<<"----------------"<<endl;   
 cout<<"4:maxsize = "<<L.Getmaxsize(L)<<endl;  // bug: what is the rand number?
/*
Linklist<char> Lc;
Lc.InsertElem(Lc,1,‘A‘);
Lc.GetElem(2,‘B‘);
Lc.ShowList(Lc);
 */
}
/********************************
       WZ  ASUST  2016
      双向链表第一阶段 
     拷贝构造中内存泄露
*********************************/
#include"sts.h"
template <class T>
struct node
{
public:node(const T &d):next(NULL),prve(NULL),data(d){}
T data;
node<T> *next;
node<T> *prve;
};
template <class T>
class dlist
{
private: node<T>* head;node<T>* tail;
public:
       dlist():head(NULL),tail(NULL){};
       ~dlist(){node<T>*cur=head;while(cur){node<T>*del=cur;cur=cur->next;delete del;}}
     void print(){node<T>*cur=head;while(cur){cout<<cur->data<< " ";cur=cur->next;}cout<<"ok"<<endl;}
    // void  dlist(const dlist<T>&dl){}//while not error
dlist & operator=(const dlist <T> & dl)
{
if(this!=&dl)
  {
   head=dl.head;
   tail=dl.tail;
   node<T>*dlcur=dl.head;
   node<T>*cur=dlcur;
int xx=4;
 while(dlcur&&xx--)
   {
   cout<<"wzzx"<<xx<<endl;
    dlcur=dlcur->next;
     dlcur=cur;
    cur=cur->next;
  cout<<cur->data<<endl;
   }
 
  }
}
void pushback(const T &d)
{
if(head==NULL)
 {
  head=new node<T>(d);
  tail=head;
 }
else 
 {
   tail->next=new node<T>(d);
   tail->next->prve=tail;
   tail=tail->next;
 }
}
T popback()
{
if(head==NULL)
 {
   return 0;
 }
else 
 {
   return tail->data;
  
 }
}
void popfront(const T &d)
{
if(head==NULL)
 {
  head=new node<T>(d);
  tail=head;
 }
else 
 {
   head->prve=new node<T>(d);
   head->prve->next=head;
   head=head->prve;
 }
}
T popfront()
{
if(head==NULL)
 {
   return 0;
 }
else 
 {
   return head->data;
  
 }
}
};
 void test()
{
dlist<int> int_dlist;
cout<<"** 1 : ***************"<<endl;
 int_dlist.pushback(11);
 int_dlist.pushback(22);
 int_dlist.pushback(33);
 int_dlist.pushback(44);
 int_dlist.print();
cout<<"*** 2 :**************"<<endl;
 
 int_dlist.popfront(55);
 int_dlist.popfront(66);
 int_dlist.popfront(77);
 int_dlist.print();
cout<<"*** 3 :**************"<<endl;
  cout<<int_dlist.popfront()<<endl;
  cout<<int_dlist.popback()<<endl;
cout<<"*** 4 :**************"<<endl;
}
int main()
{
dlist<int> int_dlist;
cout<<"** 1 : ***************"<<endl;
 int_dlist.pushback(11);
 int_dlist.pushback(22);
 int_dlist.print();
dlist<int> int_dlist2;
int_dlist2=int_dlist;
 int_dlist.print();
 return 0;
}
时间: 2024-08-01 10:43:45

C++ 模板的一个实践的相关文章

爬虫在游戏数据分析的一个实践

在实际工作中,数据的来源不能局限于自家的数据库或者成型的后台,在做某些市场分析或是竞争对手开服节奏分析的情况下,对竞争对手数据的获得显得更为至关重要,本文就以获取某大平台开服数据作为一个案例,简要的介绍另一种获取数据的方法,网络爬虫技术. 何为网络爬虫,爬虫是一个自动提取网页的程序,为搜索引擎在万维网上下载网页,是搜索引擎的重要组成部分.简单的讲就是从一个或若干个初始网页的URL开始,获得初始网页上的URL,在抓取网页的过程中,不断从当前页面上抽取新的URL放入队列,直到满足系统的某些停止条件.

T4模板的一个问题,用过的麻烦进来看看

博问里面发了几次了,看的人太少了,回答的人更少,而且都没有解决,这次发首页,希望管理手下留情,真心这个问题一个月了,一直没解决掉.高抬贵手 项目生成成功后,右键.tt文件,然后 ,然后调试T4模板成功,就OK了然而每次运行自定义工具,就会报错,应该是Machine config的错误,但是一直不会改,百度的方法也改不好,哪位老司机玩过T4模板的,帮我看看吧,问题截图发下面了 然后改过machine.config里面的内容,一直没改成功,就一直拖到现在

虚函数和模板编程的一点共性和特征模板的一个例子

最近在看元编程中,对虚函数和模板编程有一点点感悟,写一篇博客简单总结一下. 虚函数和模板是C++里面很棒的特征,他们都提供了一种方法,让程序在编译中完成一些计算,去掉的这些计算在比较low的编程方式中,是需要在程序运行中执行的.在这里,我要强调的是:"在编译过程中完成一些计算". 我会举两个例子,一个是虚函数的,比较简单,另一个例子是关于特征模板的,在例子中,根据模板参数的类型自动选择模板的底层数据结构. 第一个例子是比较简单的虚函数的例子,有很多种水果的类型,我们有一个函数要展示他们

关于模板渲染一个很重要的用途

一般情况下我们在模板利用django的for标签循环生成html代码时,可以同时生成形如: "{% url 'dormitory:hygiene_detail' pk={{ id }} %}" 的url地址,这样很容易就把参数动态的匹配到每一个url中了. 但是,如果html的代码需要js来动态的生成,再用django的for标签就不合适了. 因为for标签运用在js里,会生成大量的js代码. 因此,这就需要使用json.dumps()来把后台的dict数组转化成json数组,再在模板

nunjucks模板设计一个页面

使用nunjucks代替原来的ejs,因为这个更强大,是node中主流的模板引擎 nunjucks官网 配置使用 nunjucks 模板引擎 nunjucks 模板引擎没有对模板文件名的后缀名做特定限制 如果文件名是 a.html 则渲染的时候就需要传递 a.html 如果文件名是 b.nujs 则传递 b.nujs import express from 'express' import config from './config' import nunjucks from 'nunjucks

springboot+thymeleaf模板的一个问题,搞了一天才搞定。

thymeleaf中使用layout功能,根据官方的文档,看上去非常的简单,但是就是各种搞不定.百度谷歌都没能查到有效的方案,有些方案是需要再写代码配置. 直到后来我在pom文件中更新的Thymeleaf的版本,才搞定. 原来我开发的时候,是从springboot官网http://projects.spring.io/spring-boot/#quick-start上下载了一个工程文件,这个工程文件里面的pom引用的版本竟然是旧的. 我也不知道为啥是这样,后来复写了一些版本号才搞定.真的坑死我了

poi导出word模板项目实例(一个文件)

在页面上填写值,然后导出到word模板中,并把页面上的值带到模板中,也就是导出word文档,提前有word 的模板形式, 1.jsp 页面   <table class="formTable"> <TR> <TD class="label">会议地点</TD> <TD class="content"> <INPUT id="meetingSite" type=&

Mysql闭包表之关于国家区域的一个实践

在电商系统中,我们总是会遇到一些树形结构数据的存储需求.如地理区域.位置信息存储,地理信息按照层级划分,会分为很多层级,就拿中国的行政区域划分为例,简单的省-市-县-镇-村就要五个级别.如果系统涉及到跨境的国际贸易,那么存储的地理信息层级会更加深.那么如何正确合理地存储这些数据,并且又能很好的适应各种查询场景就成了我们需要考虑的问题,这次我们来考虑通过闭包表方案,来达到我们的存储及查询需求. 一.设计闭包表 闭包表由Closure Table翻译而来,通过父节点.子节点.两节点距离来描述一棵树空

javascript OOP编辑思想的一个实践参考

<html> <style type="text/css"> .current { background-color: red; } .dv { background-color: green; width: 200px; height: 200px; } </style> <head> <script type="text/javascript" src="change.js"><