使用iostream自带的streambuf实现,可以以__cin>>和__cout<<的方式使用。
使用方法如下:
名称 | 内容 |
__cin | 输入流对象,类似<iostream>里的cin。 |
__cout | 输出流对象,类似<iostream>里的cout。 |
__cin>> | 类似<iostream>里的cin>>,从缓冲区读各种变量,跳过空白字符。 |
__cin.get(char&_Ch) | 类似<iostream>里的cin.get(ch),将缓冲区的首个字符存进_Ch变量中。 |
__cin.get() | 类似<iostream>里的cin.get(),返回缓冲区的首个字符。 |
__cin.getline(std::string &_Str,char _End='\n') | 类似<string>里的getline(cin,str,delim),从缓冲区的首个字符开始,读到_End字符为止,存进_Str变量(最后一个参数不填默认换行符)。 |
__cout<< | 类似<iostream>里的cout<<,可以向缓冲区输出各种变量。 |
__cout.flush() | 类似<iostream>里的cout.flush(),刷新缓冲区,让里面的字符输出。 |
__endl | 类似<iostream>里的endl,刷新缓冲区并换行。 |
注意:
- 控制台调试时输入完成后需要回车+(Ctrl+Z)+回车表示结束,文件输入输出无影响。
- 需要使用__endl(换行并刷新缓冲区)或__cin.flush()(手动刷新缓冲区)来输出缓冲区的字符,否则可能导致无输出。
#ifndef _GLIBCXX_IOSTREAMBUF
#define _GLIBCXX_IOSTREAMBUF 1
#pragma GCC system_header
#include<iostream>
#include<cctype>
#include<string>
namespace std _GLIBCXX_VISIBILITY(default){
_GLIBCXX_BEGIN_NAMESPACE_VERSION
//实现IO优化
const int MAXIO=1<<20;
static std::streambuf *isb=std::cin.rdbuf();
static std::streambuf *osb=std::cout.rdbuf();
char isbuf[MAXIO],*sb_p1,*sb_p2,osbuf[MAXIO],*sb_p3=osbuf;
inline int streambuf_sgetn(){
return(sb_p1==sb_p2&&(sb_p2=(sb_p1=isbuf)+isb->sgetn(isbuf,MAXIO),sb_p1==sb_p2)?EOF:*sb_p1++);
}
inline int streambuf_sputn(int _Ch){
(sb_p3-osbuf<