使用方法和cin类似,只需要将cin换成xszq::cin即可。
#ifndef IO_H_
#define IO_H_
#if defined(__unix) \
|| defined(__unix__) \
|| defined(__linux) \
|| defined(__linux__)
#define IO_H_VERSION 1.1
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <cctype>
#include <string>
namespace xszq {
class istream {
public:
istream();
istream& operator>>(int&);
istream& operator>>(int short&);
istream& operator>>(int long&);
istream& operator>>(int long long&);
istream& operator>>(int unsigned&);
istream& operator>>(int short unsigned&);
istream& operator>>(int long unsigned&);
istream& operator>>(int long long unsigned&);
istream& operator>>(char&);
istream& operator>>(char unsigned&);
istream& operator>>(char*);
istream& operator>>(std::string&);
operator bool();
private:
char *ptr;
char *begin;
bool lastSuccess;
} cin;
istream::istream() {
ptr = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0);
begin = ptr;
lastSuccess = true;
}
#define integerUnsignedOperator(type) \
istream& istream::operator>>(type& var) { \
var = 0; \
while (*ptr && !isdigit(*ptr)) {++ ptr;} \
if (!(*ptr)) {lastSuccess = false; return *this;} \
while (*ptr && isdigit(*ptr)) {var=var*10+*ptr++-48;} \
return *this; \
}
#define integerSignedOperator(type) \
istream& istream::operator>>(type& var) { \
bool isNegative = false; \
var = 0; \
while (*ptr && !isdigit(*ptr)) {isNegative|=*ptr++=='-';} \
if (!(*ptr)) {lastSuccess = false; return *this;} \
while (*ptr && isdigit(*ptr)) {var=var*10+*ptr++-48;} \
if (isNegative) var = -var; \
return *this; \
}
integerSignedOperator(int)
integerSignedOperator(int short)
integerSignedOperator(int long)
integerSignedOperator(int long long)
integerUnsignedOperator(int unsigned)
integerUnsignedOperator(int short unsigned)
integerUnsignedOperator(int long unsigned)
integerUnsignedOperator(int long long unsigned)
istream& istream::operator>>(char& var) {
var = '\0';
while (*ptr && isspace(*ptr)) {++ ptr;}
if (!(*ptr)) {lastSuccess = false; return *this;}
var = *ptr++;
return *this;
}
istream& istream::operator>>(char unsigned& var) {
var = '\0';
while (*ptr && isspace(*ptr)) {++ ptr;}
if (!(*ptr)) {lastSuccess = false; return *this;}
var = *ptr++;
return *this;
}
istream& istream::operator>>(char *var) {
*var = '\0';
while (*ptr && isspace(*ptr)) {++ ptr;}
if (!(*ptr)) {lastSuccess = false; return *this;}
while (*ptr && !isspace(*ptr)) {*var = *ptr; ++ var; ++ ptr;}
*var = '\0';
return *this;
}
istream& istream::operator>>(std::string& var) {
var = "";
while (*ptr && isspace(*ptr)) {++ ptr;}
if (!(*ptr)) {lastSuccess = false; return *this;}
while (*ptr && !isspace(*ptr)) {var += *ptr; ++ ptr;}
return *this;
}
istream::operator bool() {
return lastSuccess;
}
}
#else
#error "This header file only supports Unix/Linux."
#endif
#endif