One question 22:deque and trait per day

Source: Internet
Author: User

Although previously written a deque, but that belongs to C and C + + mixed products, recently learned C + + templates, so wrote a C + + version, and the first attempt to use trait technology.
Had already written a good article, Post lost, good depressed, no mood to write again, paste code bar.

This trait is used to determine whether a parameter is passed using a value pass or a reference.

#ifndef _type_traits_h_#define _TYPE_TRAITS_H_namespacemydatastructure{Template<TypeNameT>classparametertrait {Private:Template<TypeNameU>struct__paratrait {typedefu& ParameterType; };Template<TypeNameU>struct__paratrait<u*> {typedefU ParameterType; };Template<TypeNameU>struct__paratrait<u&> {typedef TypeName__paratrait<u>::P arametertype parametertype; };Template<>struct__paratrait<Char> {typedef CharParameterType; };Template<>struct__paratrait<unsigned Char> {typedef unsigned CharParameterType; };Template<>struct__paratrait<int> {typedef intParameterType; };Template<>struct__paratrait<unsigned int> {typedef unsigned intParameterType; }; Public:typedef TypeName__paratrait<t>::P arametertype parametertype; };}#endif

Template in the case of the implementation of the double-ended queue (in fact, this is only a doubly linked list, the real double-ended queue is more complex than this):

#ifndef _deque_h_#define _deque_h_#include ". \utilites\type_traits.h "Namespace mydatastructure{Template<typename t> class Bidirectionnode {Private: TypeDef typename PARAMETERTRAIT&LT;T&GT;::P arametertype parametertype; Public:Bidirectionnode(ConstParameterTypevalue) :value(value),Next(nullptr),Pre(nullptr) {} bidirectionnode (Constbidirectionnode& N) {_copy (n); } bidirectionnode&operator= (Constbidirectionnode& N) {_copy (n);return* This; } T Value ()Const{return value; } t& Value () {return value; } t* Next () {returnNext } Public:void_Copy(Constbidirectionnode& N) {value= N.value;            Next = N.next;        Pre = N.pre; } Tvalue;        bidirectionnode* Pre;    Bidirectionnode* Next;    }; Template<typename T> class Deque {Private: TypeDef typename Bidirectionnode<t> NodeType; typedef typename PARAMETERTRAIT&LT;T&GT;::P arametertype parametertype; Public:Deque() :size(0),Head(nullptr),Tail(nullptr) {} Deque (Constdeque& d) {_copy (d);} deque&operator=(Constdeque& d) {_destroy (); _copy (d);return* This; } ~deque () {_destroy ();}voidClear () {_destroy ();}Const intSize () {returnSize }BOOLEmpty () {returnSize = =0; }voidPush (ConstParameterTypevalue) {nodetype* n =NewNodeType (value);        _push (n); }voidInsert (ConstParameterTypevalue) {nodetype* n =NewNodeType (value);        _insert (n); }BOOLPop (t&value)        {if(Size = =0)return false;value= head->value; nodetype* p = head;if(Size = =1) {head = tail = nullptr; }Else{head = head->next;            Head->pre = nullptr;            }--size; Delete p;return true; }BOOLEject (t&value)        {if(Size = =0)return false;value= tail->value; nodetype* p = tail;if(Size = =1) {head = tail = nullptr; }Else{tail = tail->pre;            Tail->next = nullptr;            }--size; Delete p;return true; }Private:void_Push(nodetype* N) {if(Size = =0) {head = n;                Head->next = nullptr;                Head->pre = nullptr;            tail = head; }Else{n->next = head;                N->pre = nullptr;                Head->pre = n;            Head = N;        } ++size; }void_insert (nodetype* N) {if(Size = =0) {head = n;                Head->next = nullptr;                Head->pre = nullptr;            tail = head; }Else{tail->next = n;                N->pre = tail;                N->next = nullptr;            tail = n;        } ++size; }void_copy (Constdeque& d) {nodetype* node = d.head; while(node) {nodetype* NewNode =NewNodeType (*node);                _insert (NewNode);            node = node->next; }        }void_destroy () {nodetype* node = head; while(node)                {nodetype* p = node->next;                Delete node;            node = p;            } head = tail = nullptr; Size =0; }Private:intSize        Nodetype* Head;    nodetype* tail; };}#endif 

Test code:

//Deque.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include "Deque.h"#include <iostream>#include <deque>using namespace STD;using namespaceMydatastructure;int_tmain (intARGC, _tchar* argv[]) {deque<int> D; for(inti =0; I <Ten;    ++i) {D.insert (i); } deque<int> d2; deque<int> D1 = d2 = D;intValue for(inti =0; I <Ten; ++i) {if(D1. Eject (value))cout<< value <<"'; }cout<< Endl; for(inti =0; I <Ten; ++i) {if(D2. Eject (value))cout<< value <<"'; }cout<< Endl; for(inti =0; I <Ten; ++i) {if(D.eject (value))cout<< value <<"'; }cout<< Endl;cout<< d.size () << Endl;    D.clear (); D1.    Clear (); D2. Clear ();return 0;}

Operation Result:

This blog experience is: 1) After the comments are written directly in the source file, not to write a blog when it was written in the blog post. 2) If you are writing half saved online, next time you finish publishing, please save and then publish, or else if you change the published article immediately, only half of the previous content. The Pit is dead!

One question 22:deque and trait per day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.