[namespace hdk] 合集 demap / vector / eint / fastio

demap 实现一个既可以正向对应也可以逆向对应的 map 结构.

代码

template<typename F,typename S>
class demap{
	private:
		std::map<F,S> f;
		std::map<S,F> s;
	public:
		void add(F Key,S Text){f[Key]=Text;s[Text]=Key;}
		S find_text(F Key){return f[Key];}
		F find_key(S Text){return s[Text];}
		bool count_key(F Key){return f.count(Key);}
		bool count_text(S Text){return s.count(Text);}
		void clear(){s.clear();f.clear();}
		bool empty(){return s.empty();}
};

使用例

int main(){
	demap<int,string> d;
	d.add(1,"GGrun");
	d.add(3,"HDK");
	d.add(2,"9G");
	cout<<d.find_text(2)<<endl;
	cout<<d.find_key("HDK")<<endl;
	cout<<d.count_text("GGrun")<<endl;
	cout<<d.count_key(4)<<endl;
	cout<<d.empty()<<endl;
	d.clear();
	cout<<d.empty()<<endl;
}

vector

  namespace hdk;
  const size_t size=100000;
	template<typename T>
	class vector{
		private:T a[size];T* begind=a;T* endd=a;
		public:
			inline void push(T x){if(endd>a+size) exit(274);*endd=x;endd++;}
			inline bool empty(){if(endd==begind) return true;return false;}
			inline void pop(){if(endd==begind) exit(275);endd--;}
			inline T back(){if(endd==begind) exit(275);return *(endd-1);}
			inline T* begin(){return begind;}
			inline T* end(){return endd+1;}
			inline T get(int* x){if(x>endd) exit(276);return *x;}
			inline int remain_size(){return size-(endd-begind);}
			T* operator [](int x){if(begind+x>endd) endd=begind+x;return begind+x;}
			inline void clear(){memset(a,0,sizeof a);endd=begind;}
	};
}

Eint 科学计数的精度计算

namespace hdk{
    const long double eps=1e-7;
	namespace SIEN_INT{
		struct Eint{
			int pow;
			long double frac;
			void clear(){pow=0;frac=0;}
			void reco(){if(frac>0){while(frac<1){if(frac-1.0<eps) break;frac*=10;pow--;}while(frac>=10){if(frac-10.0<eps) break;frac/=10;pow++;}}if(frac<0){while(frac>-1){if(fabs(frac+1.0<eps)) break;frac*=10.0;pow--;}while(frac<=-10){if(fabs(frac+10.0<eps)) break;frac/=10.0;pow++;}}}
			void print(){printf("%Lfe%d",frac,pow);}
			void input(){cin>>frac>>pow;reco();}
			void redpow(int to){while(pow!=to){pow--;frac*=10;}}
			void operator =(const pair<long double,int> x){frac=x.first;pow=x.second;reco();}
			void operator =(const int x){frac=x;pow=0;reco();}
			bool operator >(const Eint &A)const{if(pow==A.pow) return frac>A.frac;return pow>A.pow;}
			bool operator ==(const Eint &A)const{if(pow==A.pow&&fabs(frac-A.frac)<eps) return true;return false;}
			bool operator <(const Eint &A)const{return !((*this>A)||(*this==A));}
			bool operator >=(const Eint &A)const{return !(*this<A);}
			bool operator <=(const Eint &A)const{return !(*this>A);}
			Eint operator +(Eint &A){Eint ans;if(pow>A.pow) redpow(A.pow);else A.redpow(pow);ans.frac=frac+A.frac;ans.pow=pow;ans.reco();reco();A.reco();return ans;}
			void operator +=(Eint &A){*this=*this+A;}
			Eint operator -(Eint &A){Eint ans;if(pow>A.pow) redpow(A.pow);else A.redpow(pow);ans.frac=frac-A.frac;ans.pow=pow;ans.reco();reco();A.reco();return ans;}
			void operator -=(Eint &A){*this=*this-A;}
			Eint operator *(const Eint &A)const{Eint ans;ans.frac=frac*A.frac;ans.pow=pow+A.pow;ans.reco();return ans;}
			void operator *=(const Eint &A){*this=*this*A;}
			Eint operator /(const Eint &A)const{Eint ans;ans.frac=frac/A.frac;ans.pow=pow-A.pow;ans.reco();return ans;}
			void operator /=(const Eint &A){*this=(*this/A);}
			Eint operator ^(const int x){Eint ans;ans.frac=std::pow(frac,x);ans.pow=pow*x;ans.reco();return ans;}
			void operator ^=(const int x){pow*=x;}
		};
		Eint e(int x){Eint ans;ans=x;return ans;}
		pair<long double,int> E(long double _f,int _p){return make_pair(_f,_p);}
		pair<long double,int> E(int _p,long double _f){return make_pair(_f,_p);}
	}
}

Iter / fastio

namespace hdk{
    namespace Iter{
		void cout(std::vector<int> &_v,int _from,int _to,char _devide){std::vector<int>::iterator iter;std::vector<int>::reverse_iterator riter;
		if(_from<_to){for(iter=_v.begin()+_from;iter!=_v.begin()+_to;++iter){std::cout<<*iter<<_devide;}}
		else{for(riter=_v.rbegin()+_to;riter!=_v.rbegin()+_from;++riter){std::cout<<*riter<<_devide;}}}
	}
	template<typename T>
	void memset(T a[],int _val,int _size){if(_val==0){for(T* i=a;i<=a+_size-1;++i) *i&=0;return;}for(T* i=a;i<=a+_size-1;++i)*i=_val;}
	namespace fastio{
		void rule(bool setting=false){std::ios::sync_with_stdio(setting);}
		inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
	    inline int read(int &A){A=read();return A;}
        inline char read(char &A){A=getchar();return A;}
		inline void write(int A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
        inline void write(long long A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
		inline void write(char A){putchar(A);}
		inline void space(){putchar(' ');}
		inline void endl(){putchar('\n');}
		#define w(a) write(a)
		#define we(a) write(a);endl()
		#define ws(a) write(a);space()
	}
}
posted @ 2024-04-17 11:05  HaneDaniko  阅读(34)  评论(1编辑  收藏  举报