⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tool.h

📁 数据结构实例 可以学一下。。
💻 H
字号:
#pragma once
#include "mylib.h"

namespace mylib {
	class time_conuter{
	public:
		enum Stat {started, ended, uninitialized};
		time_conuter(void) : _beg(0), _total(0), _stat(uninitialized){}
		time_conuter(const time_conuter& timer): _beg(timer._beg),
                                                 _stat(timer._stat)
		{}
		const time_conuter& operator= (const time_conuter& timer)
		{
			if(this != &timer)
 			{
				_beg = timer._beg;
				_total = timer._total;
				_stat = timer._stat;
			}
			return *this;
		}
		virtual ~time_conuter(void){}
		void start(void)
		{
			_beg = clock();
			_stat = started; 
		}
		void end(void)
		{ 
			if(_stat == started)
			{	
				_total += clock() - _beg;
				_stat = ended;
			}
		}
		const clock_t total() const
		{
			return _stat == ended ? _total : -1;
		}
		void clear(void){ _total = 0 ;_stat = uninitialized;}
	private:
		Stat _stat;	
		clock_t _total;
		clock_t _beg;
	};
	template<typename Ty>
	class runtimecmp{
	public:
		enum cmp_mode{normal, reverse};

		explicit runtimecmp(cmp_mode m = normal) : mode(m) {}
		runtimecmp(const runtimecmp& cmpobj) : mode(cmpobj.mode)
		{}
		const runtimecmp& operator= (const runtimecmp& cmpobj)
		{
			if(this != &cmpobj)
				mode = cmpobj.mode;
			return *this;
		}
		virtual ~runtimecmp(){}

		const bool operator() (const Ty& t1, const Ty& t2)
		{
			return (mode == normal) ? (t1 < t2) : (t2 < t1);
		}
		const bool operator== (const runtimecmp& cmpobj)
		{
			return mode == cmpobj.mode;
		}
	private:
		cmp_mode mode;
	};

	template<typename colltype>
		void print_container(const colltype& coll,
		                     const std::string& str = "")
	{
		std::cout << str;
		typename colltype::const_iterator it;
		for(it = coll.begin(); it != coll.end(); ++it)
			std::cout << *it << "\t" ;
		std::cout << std::endl;
	}

	
	template<typename colltype>
		void file_to_container(colltype& coll,
		                       std::ifstream& fin)
	{
		std::
		istream_iterator<typename colltype::value_type> beg(fin),
			                                            end;
		std::copy(beg, end, std::inserter(coll, coll.end()));
	}

	template<typename colltype>
		void container_to_file(colltype& coll,
		                       std::ofstream& fout)
	{
		std::
		ostream_iterator<typename colltype::value_type> beg(fout,"\t");
		std::copy(coll.begin(), coll.end(), beg);
	}

	template<typename Ty>
		const Ty& print_element(const Ty& elem)
	{
		static size_t i = 0;
		if( (i!=0) && (i%4==0) )
			std::cout << std::endl;
		std::cout << elem << "\t" ;
		++i;
		return elem;
	}

	template<typename colltype>
		void insert_element(colltype& coll,
		                    typename colltype::value_type beg,
						    typename colltype::value_type end)
	{
		for(typename colltype::value_type temp = beg;
			temp <= end; ++temp)
		{
			coll.insert(coll.end(), temp);
		}
	}

	template<typename colltype>
		void look_container(const colltype& date)
	{
		std::cout << "container's type is : "
			<< typeid(colltype).name()
			<< "\n"
			<< "container's value type is : "
			<< typeid(typename colltype::value_type).name()
			<< "\n"
			<< "container's size is:"
			<< date.size()
			<< "\n"
			<< "container's max size is :"
 			<< date.max_size()
			<< std::endl;
}    

 
   template<typename Ty>
       class my_iterator
		   : std::iterator<std::output_iterator_tag,
                           void, void, void, void>
   {
      protected:
	     Ty& contiter;
      public:
	     explicit my_iterator(Ty& ct) : contiter(ct) {}

	     my_iterator<Ty>&
		    operator= (const typename Ty::value_type& value)
	     {
			 contiter.insert(value);
			 return *this;
		 }
	  
	     my_iterator<Ty>&
			 operator* (void)
		 {
			 return *this;
		 }

         my_iterator<Ty>&
			 operator++ (void)
		 {
			 return *this;
		 }

         my_iterator<Ty>&
			 operator++ (int)
		 {
			 return *this;
		 }
   };
   template<typename Ty>
	   my_iterator<Ty> insert_iterator(Ty& ct)
   {
	   return my_iterator<Ty>(ct);
   }
};
 

⌨️ 快捷键说明

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