template fastread

昨天梦到一种快读,你给他传多少参,他就读多少数

今天学了折叠表达式和隐式转换,现在来把这玩意实现一下

特化函数

void read(int) 读入一个整数
void read(char&[],range=size_t) 按数组大小读入一个字符串
void readarray(T x,T2&[]) 先读入一个整数 \(x\),再读入 \(x\) 个数字,传入数组

一般函数

void read(...Args) 读入若干变量. 仅限于整数类型,数组或字符串
void readact(int,function<>,...Args) 第一个参数为执行次数,为第三个参数及以后的全部参数执行读入,再传入第二个参数内执行.

使用例

读入下列数据:

1

5
1 2 3 4 5
int n,a[6];
readarray(n,a);

2

1 3 5
int u,v,w;
read(u,v,w);
int a[4];
read(a,3);

3
图论建边

4 3
1 2
2 3
3 4
read(n,m)
for(int i=1;i<=m;++i){
    read(u,v,w);add(u,v,w);
}
function<auto> r=add;
read(n,m);readact(m,r,u,v,w);

代码

#include<bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T& x){
	x=0;bool sym=0;char c=getchar();
    while(!isdigit(c)){sym^=(c=='-');c=getchar();}
    while(isdigit(c)){x=x*10+c-48;c=getchar();}
    if(sym)x=-x;
}
template<size_t N>
inline void read(char (&str)[N]){
    size_t n=0;char c=getchar();
    while(n<N-1&&!isspace(c)){str[n]=c;c=getchar();++n;}
    str[n]=0;
}
template<typename T,size_t N>
inline void read(T (&a)[N],int range=N){
	for(int i=1;i<=range-1;++i){read(a[i]);}
}
template<typename T,typename... Args>
inline void read(T& x,Args&... args){
    read(x);read(args...);
}
template<typename T,typename T2>
inline void readarray(T& x,T2& args){
	read(x);read(args,x);
}
template<typename func,typename... Args>
inline void readact(int x,function<func>fu,Args&... args){
	for(int i=1;i<=x;++i){
		read(args...);
		fu(args...);
	}
}

cpp.json

	{
		"Template":{
			"prefix": "fastread",
			"body":[
				"#define endl '\\n'",
				"template<typename T>",
				"inline void read(T& x){",
				"    x=0;bool sym=0;char c=getchar();",
				"    while(!isdigit(c)){sym^=(c=='-');c=getchar();}",
				"    while(isdigit(c)){x=x*10+c-48;c=getchar();}",
				"    if(sym)x=-x;",
				"}",
				"template<size_t N>",
				"inline void read(char (&str)[N]){",
				"    size_t n=0;char c=getchar();",
				"    while(n<N-1&&!isspace(c)){str[n]=c;c=getchar();++n;}",
				"    str[n]=0;",
				"}",
				"template<typename T,size_t N>",
				"    inline void read(T (&a)[N],int range=N){",
				"    for(int i=1;i<=range-1;++i){read(a[i]);}",
				"}",
				"template<typename T,typename... Args>",
				"    inline void read(T& x,Args&... args){",
				"    read(x);read(args...);",
				"}",
				"template<typename T,typename T2>",
				"inline void readarray(T& x,T2& args){",
				"    read(x);read(args,x);",
				"}",
				"template<typename func,typename... Args>",
				"inline void readact(int x,function<func>fu,Args&... args){",
				"    for(int i=1;i<=x;++i){",
				"        read(args...);",
				"        fu(args...);",
				"    }",
				"}"
			]
		}
	}
	// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
posted @ 2024-08-09 09:59  HaneDaniko  阅读(17)  评论(0编辑  收藏  举报