---
# System prepended metadata

title: iostreambuf
tags: [codebook]

---

---
tags: codebook
---
{%hackmd theme-dark %}
# iostreambuf
```cpp=
#include<iostream> 
#include<streambuf>
#include<cctype>
#include<limits>
using namespace std;
struct istreambuf{
	bool eof;streambuf *ib;size_t _last_input;
	operator bool(){return !eof;}
//get
	inline void tie(int*){return;}
	inline int get(){int c=ib->sbumpc();return c;}
	inline istreambuf& get(char& c){c=ib->sbumpc();return *this;}
	inline size_t gcount(){return _last_input;}
//ignore
	inline istreambuf& ignore(size_t n=1,int delim=EOF){
		int c=ib->sbumpc();while(n--&&c!=delim&&c!=EOF)c=ib->sbumpc();
		if(c==EOF){eof=true;}return *this;}
//getline
	inline istreambuf& getline(char* s,size_t n,char delim=EOF){
		_last_input=0;char c=ib->sbumpc();
		while(c!='\n'&&c!=EOF&&c!=delim&&_last_input!=n){
			*(s+_last_input)=c;c=ib->sbumpc();_last_input++;};if(c==EOF){eof=true;}return *this;}
//getline(friend)
	friend inline istreambuf& getline(istreambuf& buf,string& str,char delim=EOF){
		str.clear();char c=buf.ib->sbumpc();
		while(c!='\n'&&c!=EOF&&c!=delim){str.push_back(c);c=buf.ib->sbumpc();}
		buf._last_input=str.size();if(c==EOF){buf.eof=true;}return buf;}
//operator>>
	inline istreambuf& operator>>(unsigned& re){
		re=0;char c=0;while(!isdigit(c)){if(c==EOF){eof=true;return *this;}c=ib->sbumpc();}
		if(c==EOF){eof=true;return *this;}
		while(isdigit(c)){re=re*10+c-'0',c=ib->sbumpc();}return *this;}
	inline istreambuf& operator>>(unsigned long long& re){
		re=0;char c=0;while(!isdigit(c)){if(c==EOF){eof=true;return *this;}c=ib->sbumpc();}
		while(isdigit(c)){re=re*10+c-'0',c=ib->sbumpc();}return *this;}
	inline istreambuf& operator>>(long unsigned& re){
		re=0;char c=0;while(!isdigit(c)){if(c==EOF){eof=true;return *this;}c=ib->sbumpc();}
		while(isdigit(c)){re=re*10+c-'0',c=ib->sbumpc();}return *this;}
	inline istreambuf& operator>>(int& re){
		re=0;bool b=false;char c=ib->sbumpc();
		while(!isdigit(c)){if(c==EOF){eof=true;return *this;}b=(c=='-'?true:b);c=ib->sbumpc();}
		while(isdigit(c)){re=re*10+c-'0',c=ib->sbumpc();}
		re=b?-re:re; return *this;}
	inline istreambuf& operator>>(long long& re){
		re=0;bool b=false;char c=ib->sbumpc();
		while(!isdigit(c)){if(c==EOF){eof=true;return *this;}b=(c=='-'?true:b);c=ib->sbumpc();}
		while(isdigit(c)){re=re*10+c-'0',c=ib->sbumpc();}
		re=b?-re:re;return *this;}
	inline istreambuf& operator>>(float& re){cin>>re;return *this;}
	inline istreambuf& operator>>(double& re){cin>>re;return *this;}
	inline istreambuf& operator>>(long double& re){cin>>re;return *this;}
	inline istreambuf& operator>>(string& re){
		re.clear();char c=ib->sbumpc();
		while(isspace(c)) c=ib->sbumpc();
		if(c==EOF){eof=true;return *this;}
		while(!isspace(c)){re.push_back(c),c=ib->sbumpc();}
		_last_input=re.size();return *this;}
	inline istreambuf& operator>>(char& x){
		x=ib->sbumpc();while(isspace(x)) x=ib->sbumpc();
		if(x==EOF){eof=true;}return *this;}
//constructor
	istreambuf(istream& is):eof(false),_last_input(0){is.tie(nullptr);
		ios::sync_with_stdio(false);ib=is.rdbuf();}
} ibuf(cin);
struct ostreambuf{
	streambuf *ob;
	inline void tie(int*){return;}
//write
	inline ostreambuf& write(char* s,size_t n){
		for(size_t i=0;i!=n;i++){*this<<*(s+i);}return *this;}
//operator<<
	inline ostreambuf& operator<<(unsigned x){
		if(!x){ob->sputc('0');return *this;}
		char tmp[10];int cnt=0;
		while(x) tmp[cnt++]=x%10+'0',x/=10;
		while(cnt--){ob->sputc(tmp[cnt]);} return *this;}
	inline ostreambuf& operator<<(unsigned long long x){
		if(!x){ob->sputc('0');return *this;}
		char tmp[19];int cnt=0;
		while(x) tmp[cnt++]=x%10+'0',x/=10;
		while(cnt--){ob->sputc(tmp[cnt]);}return *this;}
	inline ostreambuf& operator<<(long unsigned x){
		if(!x){ob->sputc('0');return *this;}
		char tmp[19];int cnt=0;
		while(x) tmp[cnt++]=x%10+'0',x/=10;
		while(cnt--){ob->sputc(tmp[cnt]);}return *this;}
	inline ostreambuf& operator<<(int x){
		if(!x){ob->sputc('0');return *this;}
		char tmp[10];int cnt=0;bool b=false;
		if(x&numeric_limits<int>::min()) tmp[cnt++]=-(x%10)+'0',x/=-10,b=true;
		while(x) tmp[cnt++]=x%10+'0',x/=10;
		if(b){ob->sputc('-');}while(cnt--){ob->sputc(tmp[cnt]);}return *this;}
	inline ostreambuf& operator<<(long long x){
		if(!x){ob->sputc('0');return *this;}
		char tmp[19];int cnt=0;bool b=false;
		if(x&numeric_limits<long long>::min()) tmp[cnt++]=-(x%10)+'0',x/=-10,b=true;
		while(x) tmp[cnt++]=x%10+'0',x/=10;
		if(b){ob->sputc('-');}while(cnt--){ob->sputc(tmp[cnt]);}return *this;}
	inline ostreambuf& operator<<(const float& x){cout<<x;return *this;}
	inline ostreambuf& operator<<(const double& x){cout<<x;return *this;}
	inline ostreambuf& operator<<(const long double& x){cout<<x;return *this;}
	inline ostreambuf& operator<<(const string& x){ob->sputn(x.data(),x.size());return *this;}
	inline ostreambuf& operator<<(char* x){while(*x) ob->sputc(*x),x++;return *this;}
	inline ostreambuf& operator<<(char x){ob->sputc(x);return *this;}
	inline ostreambuf& operator<<(ostreambuf& (*func)(ostreambuf&)){return (*func)(*this);}
	ostreambuf& operator<<(ostream& (*func)(ostream&));
//constructor
	ostreambuf(ostream& os){os.tie(nullptr);ios::sync_with_stdio(false);ob=os.rdbuf();}
}obuf(cout);
//global function
inline ostreambuf& _newline(ostreambuf& sb){sb.ob->sputc('\n');return sb;}
//[[deprecated]] 
inline ostreambuf& _flush(ostreambuf& sb){cout<<flush;return sb;}
//[[deprecated]] 
inline ostreambuf& _newline_flush(ostreambuf& sb){sb.ob->sputc('\n');cout<<flush;return sb;}
inline ostreambuf& ostreambuf::operator<<(ostream& (*func)(ostream&)){return _newline_flush(*this);}
//macro
#define cin ibuf
#define cout obuf
#define newline _newline
#define flush _flush
#define ostream ostreambuf
#define istream istreambuf
```

```cpp=
#include<iostream>
#include<vector>
```
```cpp=
#ifdef _GLIBCXX_IOSTREAM
	#include<streambuf>
	#include<cctype>
	#include<limits>
	using namespace std;
	struct istreambuf{
		bool eof;
		streambuf *ib;
		size_t _last_input;
		operator bool(){return !eof;}
		inline void tie(int*){return;}
		inline size_t gcount(){return _last_input;}
	//get
		inline int get(){
			int c=ib->sbumpc();
			return c;
		}
		inline istreambuf& get(char& c){
			c=ib->sbumpc();
			return *this;
		}
	//ignore
		inline istreambuf& ignore(size_t n=1,int delim=EOF){
			int c=ib->sbumpc();
			while(n--&&c!=delim&&c!=EOF)
				c=ib->sbumpc();
			if(c==EOF)
				eof=true;
			return *this;
		}
	//getline
		inline istreambuf& getline(char* s,size_t n,char delim=EOF){
			_last_input=0;
			char c=ib->sbumpc();
			while(c!='\n'&&c!=EOF&&c!=delim&&_last_input!=n){
				*(s+_last_input)=c;
				c=ib->sbumpc();
				_last_input++;
			};
			if(c==EOF)
				eof=true;
			return *this;
		}
	//getline(friend)
		friend inline istreambuf& getline(istreambuf& buf,string& str,char delim=EOF){
			str.clear();
			char c=buf.ib->sbumpc();
			while(c!='\n'&&c!=EOF&&c!=delim){
				str.push_back(c);
				c=buf.ib->sbumpc();
			}
			buf._last_input=str.size();
			if(c==EOF)
				buf.eof=true;
			return buf;
		}
	//operator>>
		inline istreambuf& operator>>(unsigned& re){
			re=0;
			char c=0;
			while(!isdigit(c)){
				if(c==EOF){
					eof=true;
					return *this;
				}
				c=ib->sbumpc();
			}
			if(c==EOF){
				eof=true;
				return *this;
			}
			while(isdigit(c))
				re=re*10+c-'0',c=ib->sbumpc();
			return *this;
		}
		inline istreambuf& operator>>(unsigned long long& re){
			re=0;
			char c=0;
			while(!isdigit(c)){
				if(c==EOF){
					eof=true;
					return *this;
				}
				c=ib->sbumpc();
			}
			while(isdigit(c))
				re=re*10+c-'0',c=ib->sbumpc();
			return *this;
		}
		inline istreambuf& operator>>(long unsigned& re){
			re=0;
			char c=0;
			while(!isdigit(c)){
				if(c==EOF){
					eof=true;
					return *this;
				}
				c=ib->sbumpc();
			}
			while(isdigit(c))
				re=re*10+c-'0',c=ib->sbumpc();
			return *this;
		}
		inline istreambuf& operator>>(int& re){
			re=0;
			bool b=false;
			char c=ib->sbumpc();
			while(!isdigit(c)){
				if(c==EOF){
					eof=true;
					return *this;
				}
				b=(c=='-'?true:b);
				c=ib->sbumpc();}
			while(isdigit(c))
				re=re*10+c-'0',c=ib->sbumpc();
			re=b?-re:re;
			return *this;
		}
		inline istreambuf& operator>>(long long& re){
			re=0;
			bool b=false;
			char c=ib->sbumpc();
			while(!isdigit(c)){
				if(c==EOF){
					eof=true;
					return *this;
				}
				b=(c=='-'?true:b);
				c=ib->sbumpc();
			}
			while(isdigit(c))
				re=re*10+c-'0',c=ib->sbumpc();
			re=b?-re:re;
			return *this;
		}
		inline istreambuf& operator>>(float& re){
			cin>>re;
			return *this;
		}
		inline istreambuf& operator>>(double& re){
			cin>>re;
			return *this;
		}
		inline istreambuf& operator>>(long double& re){
			cin>>re;
			return *this;
		}
		inline istreambuf& operator>>(string& re){
			re.clear();
			char c=ib->sbumpc();
			while(isspace(c)) 
				c=ib->sbumpc();
			if(c==EOF){
				eof=true;
				return *this;
			}
			while(!isspace(c))
				re.push_back(c),c=ib->sbumpc();
			_last_input=re.size();
			return *this;
		}
		inline istreambuf& operator>>(char& x){
			x=ib->sbumpc();
			while(isspace(x))
				x=ib->sbumpc();
			if(x==EOF)
				eof=true;
			return *this;
		}
	//constructor
		istreambuf(istream& is):eof(false),_last_input(0){
			is.tie(nullptr);
			ios::sync_with_stdio(false);
			ib=is.rdbuf();
		}
	} ibuf(cin);
	struct ostreambuf{
		streambuf *ob;
		inline void tie(int*){return;}
	//write
		inline ostreambuf& write(char* s,size_t n){
			for(size_t i=0;i!=n;i++)
				*this<<*(s+i);
			return *this;
		}
	//operator<<
		inline ostreambuf& operator<<(unsigned x){
			if(!x){
				ob->sputc('0');
				return *this;
			}
			char tmp[10];
			int cnt=0;
			while(x) 
				tmp[cnt++]=x%10+'0',x/=10;
			while(cnt--)
				ob->sputc(tmp[cnt]);
			return *this;
		}
		inline ostreambuf& operator<<(unsigned long long x){
			if(!x){
				ob->sputc('0');
				return *this;
			}
			char tmp[19];
			int cnt=0;
			while(x) 
				tmp[cnt++]=x%10+'0',x/=10;
			while(cnt--)
				ob->sputc(tmp[cnt]);
			return *this;
		}
		inline ostreambuf& operator<<(long unsigned x){
			if(!x){
				ob->sputc('0');
				return *this;
			}
			char tmp[19];
			int cnt=0;
			while(x)
				tmp[cnt++]=x%10+'0',x/=10;
			while(cnt--)
				ob->sputc(tmp[cnt]);
			return *this;
		}
		inline ostreambuf& operator<<(int x){
			if(!x){
				ob->sputc('0');
				return *this;
			}
			char tmp[10];
			int cnt=0;
			bool b=false;
			if(x&numeric_limits<int>::min())
				tmp[cnt++]=-(x%10)+'0',x/=-10,b=true;
			while(x)
				tmp[cnt++]=x%10+'0',x/=10;
			if(b)
				ob->sputc('-');
			while(cnt--)
				ob->sputc(tmp[cnt]);
			return *this;
		}
		inline ostreambuf& operator<<(long long x){
			if(!x){
				ob->sputc('0');
				return *this;
			}
			char tmp[19];
			int cnt=0;
			bool b=false;
			if(x&numeric_limits<long long>::min())
				tmp[cnt++]=-(x%10)+'0',x/=-10,b=true;
			while(x)
				tmp[cnt++]=x%10+'0',x/=10;
			if(b)
				ob->sputc('-');
			while(cnt--)
				ob->sputc(tmp[cnt]);
			return *this;
		}
		inline ostreambuf& operator<<(const float& x){
			cout<<x;
			return *this;
		}
		inline ostreambuf& operator<<(const double& x){
			cout<<x;
			return *this;
		}
		inline ostreambuf& operator<<(const long double& x){
			cout<<x;
			return *this;
		}
		inline ostreambuf& operator<<(const string& x){
			ob->sputn(x.data(),x.size());
			return *this;
		}
		inline ostreambuf& operator<<(char* x){
			while(*x) ob->sputc(*x),x++;
			return *this;
		}
		inline ostreambuf& operator<<(char x){
			ob->sputc(x);
			return *this;
		}
		inline ostreambuf& operator<<(ostreambuf& (*func)(ostreambuf&)){
			return (*func)(*this);
		}
		ostreambuf& operator<<(ostream& (*func)(ostream&));
	//constructor
		ostreambuf(ostream& os){
			os.tie(nullptr);
			ios::sync_with_stdio(false);
			ob=os.rdbuf();
		}
	}obuf(cout);
	//global function
	inline ostreambuf& _newline(ostreambuf& sb){
		sb.ob->sputc('\n');
		return sb;
	}
	//[[deprecated]] 
	inline ostreambuf& _flush(ostreambuf& sb){
		cout<<flush;
		return sb;
	}
	//[[deprecated]] 
	inline ostreambuf& _newline_flush(ostreambuf& sb){
		sb.ob->sputc('\n');
		cout<<flush;
		return sb;
	}
	inline ostreambuf& ostreambuf::operator<<(ostream& (*func)(ostream&)){
		return _newline_flush(*this);
	}
	//macro
	#define cin ibuf
	#define cout obuf
	#define newline _newline
	#define flush _flush
	#define ostream ostreambuf
	#define istream istreambuf
	#define endl '\n'
	#ifdef _GLIBCXX_VECTOR
		#include<vector>
		template<typename T>
		ostream& operator<<(ostream& os,vector<T>& v){
			for(const auto& i:v)
				os<<i<<' ';
			return os;
		}
		template<typename T>
		istream& operator>>(istream& is,vector<T>& v){
			for(auto& i:v) 
				is>>i;
			return is;
		}
	#endif
#endif
```

## rit/wit

```cpp=
#include<iostream>
#include<cctype>
#include<streambuf>
#include<limits>
using namespace std;
inline bool rit(int& re) {
	re = 0; bool b = false; char c = cin.rdbuf()->sbumpc();
	while (!isdigit(c)) {
		if (c == EOF)
			return false;
		b = (c == '-' ? true : b);
		c = cin.rdbuf()->sbumpc();
	}
	while (isdigit(c))
		re = re * 10 + c - '0', c = cin.rdbuf()->sbumpc();
    re = b ? -re : re;
    return true;
}
inline bool rit(unsigned long long& re) {
	re = 0; char c = cin.rdbuf()->sbumpc();
	while (!isdigit(c)) {
		if (c == EOF)
			return false;
		c = cin.rdbuf()->sbumpc();
	}
	while (isdigit(c))
		re = re * 10 + c - '0', c = cin.rdbuf()->sbumpc();
	return true;
}
inline void wit(int x) {
	if (!x) cout.rdbuf()->sputc('0');
	char tmp[10]; int cnt = 0; bool b = false;
	if (x & numeric_limits<int>::min()) tmp[cnt++] = -(x % 10) + '0', x /= -10, b = true;
	while (x) tmp[cnt++] = x % 10 + '0', x /= 10;
	if(b) cout.rdbuf()->sputc('-');
	while (cnt--) cout.rdbuf()->sputc(tmp[cnt]);
	cout.rdbuf()->sputc('\n');
}
inline void wit(unsigned long long x) {
	if (!x) cout.rdbuf()->sputc('0');
	char tmp[19]; int cnt = 0;
	while (x) tmp[cnt++] = x % 10 + '0', x /= 10;
	while (cnt--) cout.rdbuf()->sputc(tmp[cnt]);
	cout.rdbuf()->sputc('\n');
}
```