xstl.h

来自「该程序是用vc开发的对动态数组进行管理的DLL」· C头文件 代码 · 共 759 行

H
759
字号
#ifndef XSTL_H__ 
#define XSTL_H__

#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <exception>
#include <algorithm>
#include <complex>

using namespace std;

////////////////////////////////////////////////
///////////  class XStack container
///////////////////////////////////////////////
template <class T> 
class XStack
{
public:
	std::deque<T>c;
	// destructor
	~XStack()
	{
		c.clear();
	}
	
 	// constructor
	 XStack():c()
	{
	}

 	// get size
	typename std::deque<T>::size_type  size()const
	{
		return c.size();
	}

	// is empty or not
	bool  is_empty()const
	{
		return c.empty();
	}

 	// push
	void  push(const T & elem)
	{
		c.push_back(elem);
	}

 	// pop
	T  pop()
	{
		if(c.empty())
		{
			 throw ReadEmptyStack();
		}
		T elem(c.back());
		c.pop_back();
		return elem;
	}

 	// top value reference
	T &  top()
	{
		if(c.empty())
		{
			 throw ReadEmptyStack();
		}
		return c.back();
	}

	// exception
	class XReadEmptyStack:public std::exception
	{
	public:
		virtual const char * what() const throw()
		{
			return "Read empty stack";
		}
	};

	
};
  

////////////////////////////////////////////////
///////////  class XQueque container
///////////////////////////////////////////////
template <class T>  
class XQueque
{
public:
	// data
	std::deque<T>c;
	
	// deconstructor
	~XQueque()
	{
		c.clear();
	}
 
 	// constructor
	XQueque():c()
	{
	}
 
 	// get size
	typename std::deque<T>::size_type  size()const
	{
		return c.size();
	}

 	// is empty or not
	bool  is_empty()const
	{
		return c.empty();
	}

 	// push
	void  push(const T & elem)
	{
		c.push_back(elem);
	}

 	// pop
	T  pop()
	{
		if(c.empty())
		{
			 throw ReadEmptyStack();
		}
		T elem(c.front());
		c.pop_front();
		return elem;
	}

 	// get front reference
	T &  front()
	{
		if(c.empty())
		{
			 throw ReadEmptyStack();
		}
		return c.front();
	}

	// exceptions
	class XReadEmptyStack:public std::exception
	{
	public:
		virtual const char * what() const throw()
		{
			return "Read empty Queque";
		}
	};
};



////////////////////////////////////////////////
///////////  class XArray container
///////////////////////////////////////////////
template <class T> 
class XArray
{
public:
	// data
	std::deque<T> c;
	
	// constructor
	XArray():c()
	{
	}
 	
 	// destructor
	~XArray()
	{
		c.clear();
	}
 	
 	// get size
	typename std::deque<T>::size_type  size()const
	{
		return c.size();
	}

 	// is empty or not
	bool  isEmpty()const
	{
		return c.empty();
	}

 	// get iterator at npos
	deque<T>::iterator  it(int npos)
	{
		int i,n = size();
		if( npos <= 0)npos = 0;
		if( npos > n ) npos = n;
		deque<T>::iterator it1;
		for( it1 = c.begin(),i = 0; it1 != c.end(); ++ it1, ++ i )
		{
			if( i == npos )
				break;
		}
		return it1;	
	}

 	// insert element at begin
	void  addBeg(const T & elem)
	{
		c.push_front(elem);
	}
 
 	//insert element at npos
	void  addCur(const T & elem,int npos)
	{
		c.insert(it(npos),elem);
	}
 
 	// insert element at end
	void  addEnd(const T & elem)
	{
		c.push_back(elem);
	}

  	// insert array at begin
	void  addBeg(const XArray<T> & array)
	{
		c.insert(c.begin(),array.c.begin(),array.c.end());
	}

 	// insert array at end
	void  addEnd(const XArray<T> & array)
	{
		c.insert(c.end(),array.c.begin(),array.c.end());
	}

 	// insert array at npos
	void  addCur(const XArray<T> & array,int npos)
	{
		c.insert(it(npos),array.c.begin(),array.c.end());
	}

	// insert elements [from arrayFrom to arrayTo in array] at npos
	void  addCur(const XArray<T> & array,int arrayFrom,int arrayTo,int npos)
	{
		c.insert(it(npos),array.it(arrayFrom),array.it(arrayTo));		
	}

	// remove at begin
	void  delBeg()
	{
		if( size() > 0 )
		c.pop_front();
	}
 
 	// remove at end
	void  delEnd()
	{
		if( size() > 0 )
		c.pop_back();
	}

 	// remove at npos
	void  delCur(int npos)
	{
		int i,n = size();
		if(n <=0 )return;
		if( npos < 0)return;
		if( npos > n - 1 ) return ;
		deque<T>::iterator it;
		for( it = c.begin(),i = 0; it != c.end(); ++ it, ++ i )
		{
			if( i == npos )
			{
				c.erase(it);
				break;
			}
		}
	}

 	// remove all
	void  delAll()
	{
		c.clear();
	}

 	// get begin element reference
	T &   Beg()
	{
		if(isEmpty())
		{
			throw ReadEmptyStack();
		}
		return c.front();
	}

  	// get end element reference
	T  &  End()
	{
		if(isEmpty())
		{
			throw ReadEmptyStack();
		}
		return c.back();
	}

  	// get  element reference at npos
	T &   At(int npos)
	{
		if(isEmpty())
		{
			throw ReadEmptyStack();
		}
		if( npos < 0)npos = 0;
		if( npos > size() -1 ) npos = size() - 1;
		return c.at(npos);
	}

   	// get  element reference at npos
	T &   Cur(int npos)
	{
		return At(npos);
	}
	
   	// get  element reference at npos
	T  &  operator[](int npos)
	{
		return At(npos);
	}

	// exception
	class XReadEmptyStack:public std::exception
	{
	public:
		virtual const char * what() const throw()
		{
			return "Read empty Queque";
		}
	};
};




#endif






/*
#ifndef XSTL_H__ 
#define XSTL_H__

#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <exception>
#include <algorithm>
#include <complex>


using namespace std;

template <class T>
class XStack
{
public:

	std::deque<T>c;
	XStack();
	~XStack();

	// size(), is_empty(), pop(), top(), push()
	typename std::deque<T>::size_type size()const;
	bool is_empty()const;
	void push(const T & elem);
	T    pop();
	T &  top();

	class ReadEmptyStack:public std::exception
	{
	public:
		virtual const char * what() const throw()
		{
			return "Read empty stack";
		}
	};
};
  


template <class T> 
class XQueque
{
public:

	std::deque<T>c;
	XQueque();
	~XQueque();

	// size(), is_empty(), pop(), front(), push()
	typename std::deque<T>::size_type size()const;
	bool is_empty()const;
	void push(const T & elem);
	T    pop();
	T &  front();

	class ReadEmptyStack:public std::exception
	{
	public:
		virtual const char * what() const throw()
		{
			return "Read empty Queque";
		}
	};
};




template <class T>
class XArray
{
public:

	std::deque<T> c;
	XArray();
	~XArray();

	// size(), is_empty()
	typename std::deque<T>::size_type size()const;
	bool isEmpty()const;
	
	// Growing members
	void addBeg(const T & elem);
	void addEnd(const T & elem);
	void addCur(const T & elem,int npos);

	
	void addBeg(const XArray<T> & array);
	void addEnd(const XArray<T> & array);
	void addCur(const XArray<T> & array,int npos);	
	void addCur(const XArray<T> & array,int arrayFrom,int arrayTo,int npos);	

	//   delete members
	void delBeg();
	void delEnd();
	void delCur(int npos);
	void delAll();

	// element access
	T  &  Beg();
	T  &  End();
	T  &  Cur(int pos);
	T  &  At(int pos);
	T  &  at(int pos);
	T  &  operator[](int pos);

	// return iterator
	deque<T>::iterator  it(int npos);

	// exception
	class ReadEmptyStack:public std::exception
	{
	public:
		virtual const char * what() const throw()
		{
			return "Read empty Queque";
		}
	};
};



template <class T>
XStack<T>::~XStack()
{
	c.clear();
}
template <class T>
XStack<T>::XStack():c()
{
}

template <class T>
typename std::deque<T>::size_type XStack<T>::size()const
{
	return c.size();
}

template <class T>
bool XStack<T>::is_empty()const
{
	return c.empty();
}

template <class T>
void XStack<T>::push(const T & elem)
{
	c.push_back(elem);
}

template <class T>
T XStack<T>::pop()
{
	if(c.empty())
	{
		 throw ReadEmptyStack();
	}
	T elem(c.back());
	c.pop_back();
	return elem;
}

template <class T>
T & XStack<T>::top()
{
	if(c.empty())
	{
		 throw ReadEmptyStack();
	}
	return c.back();
}


//////////////////////
// class XQueque
//////////////////////

template <class T>
XQueque<T>::~XQueque()
{
	c.clear();
}
template <class T>
XQueque<T>::XQueque():c()
{
}
template <class T>
typename std::deque<T>::size_type XQueque<T>::size()const
{
	return c.size();
}

template <class T>
bool XQueque<T>::is_empty()const
{
	return c.empty();
}

template <class T>
void XQueque<T>::push(const T & elem)
{
	c.push_back(elem);
}

template <class T>
T XQueque<T>::pop()
{
	if(c.empty())
	{
		 throw ReadEmptyStack();
	}
	T elem(c.front());
	c.pop_front();
	return elem;
}

template <class T>
T & XQueque<T>::front()
{
	if(c.empty())
	{
		 throw ReadEmptyStack();
	}
	return c.front();
}

//////////////////////
// class XArray
//////////////////////

template <class T>
XArray<T>::XArray():c()
{
	
}
template <class T>
XArray<T>::~XArray()
{
	c.clear();
}
template <class T>
typename std::deque<T>::size_type XArray<T>::size()const
{
	return c.size();
}

template <class T>
bool XArray<T>::isEmpty()const
{
	return c.empty();
}

template <class T>
deque<T>::iterator XArray<T>::it(int npos)
{
	int i,n = size();
	if( npos <= 0)npos = 0;
	if( npos > n ) npos = n;
	deque<T>::iterator it1;
	for( it1 = c.begin(),i = 0; it1 != c.end(); ++ it1, ++ i )
	{
		if( i == npos )
			break;
	}
	return it1;	
}

template <class T>
void XArray<T>::addBeg(const T & elem)
{
	c.push_front(elem);
}
template <class T>
void XArray<T>::addCur(const T & elem,int npos)
{
	c.insert(it(npos),elem);
}
template <class T>
void XArray<T>::addEnd(const T & elem)
{
	c.push_back(elem);
}

template <class T>
void XArray<T>::addBeg(const XArray<T> & array)
{
	c.insert(c.begin(),array.c.begin(),array.c.end());
}

template <class T>
void XArray<T>::addEnd(const XArray<T> & array)
{
	c.insert(c.end(),array.c.begin(),array.c.end());
}

template <class T>
void XArray<T>::addCur(const XArray<T> & array,int npos)
{
	c.insert(it(npos),array.c.begin(),array.c.end());
}

template <class T>
void XArray<T>::addCur(const XArray<T> & array,int arrayFrom,int arrayTo,int npos)
{
	c.insert(it(npos),array.it(arrayFrom),array.it(arrayTo));		
}


template <class T>
void XArray<T>::delBeg()
{
	if( size() > 0 )
	c.pop_front();
}
template <class T>
void XArray<T>::delEnd()
{
	if( size() > 0 )
	c.pop_back();
}

template <class T>
void XArray<T>::delCur(int npos)
{
	int i,n = size();
	if(n <=0 )return;
	if( npos < 0)return;
	if( npos > n - 1 ) return ;
	deque<T>::iterator it;
	for( it = c.begin(),i = 0; it != c.end(); ++ it, ++ i )
	{
		if( i == npos )
		{
			c.erase(it);
			break;
		}
	}
}

template <class T>
void XArray<T>::delAll()
{
	c.clear();
}

template <class T>
T &  XArray<T>::Beg()
{
	if(isEmpty())
	{
		throw ReadEmptyStack();
	}
	return c.front();

}

template <class T>
T  & XArray<T>::End()
{
	if(isEmpty())
	{
		throw ReadEmptyStack();
	}
	return c.back();
}

template <class T>
T &  XArray<T>::At(int npos)
{
	if(isEmpty())
	{
		throw ReadEmptyStack();
	}
	if( npos < 0)npos = 0;
	if( npos > size() -1 ) npos = size() - 1;
	return c.at(npos);
}

template <class T>
T &  XArray<T>::Cur(int npos)
{
	return At(npos);
}
template <class T>
T  & XArray<T>::operator[](int npos)
{
	return At(npos);
}



#endif

*/



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?