1.前言
cpp由于效率高 在算法等领域应用广泛(其实都广泛),但是cpp的输入与输出有丶狗,所以我们封装好一份cpp的输入输出和万能头文件,大家可以直接复制导入使用。
目的
让不熟悉cpp的人快速使用cpp写出输入输出
特点
封装好了,简单易懂,拿来就用
不足
对类的支持比较少 只支持几个基本类型
2.CV部分
everything.h
头文件 可以直接cv下来然后导入
#ifndef EVERYTHING
#define EVERYTHING
//防止重复包含
//头文件
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
//命名空间
using namespace std;
/* input:获取输入
参数:
name:要输入的变量提示词
val:要输入的变量
typeshow:是否提示类型(默认否 可省)
time:输入个数(只在vector和map中有效)
返回:
bool:描述输入是否有效
目前支持的类型:
double
float
int
long
long long
string
char
vector<(前7个类型)>
map<(前7个类型),(前7个类型)>
*/
//未定义类型的输出
template <typename T>
bool input(string name, T &val, bool typeshow = false) {
cout << "尝试输入input未定义的类型" << endl;
return false;
}
bool input(string name, double &val, bool typeshow = false) {
cout << "输入" << name << ":";
cin >> val;
if (typeshow) {
cout << "(为double)";
}
return true;
}
bool input(string name, float &val, bool typeshow = false) {
cout << "输入" << name << ":";
if (typeshow) {
cout << "(为float)";
}
cin >> val;
return true;
}
bool input(string name, int &val, bool typeshow = false) {
cout << "输入" << name;
if (typeshow) {
cout << "(为int)";
}
cout << ":" ;
cin >> val;
return true;
}
bool input(string name, short &val, bool typeshow = false) {
cout << "输入" << name;
if (typeshow) {
cout << "(为short)";
}
cout << ":" ;
cin >> val;
return true;
}
bool input(string name, long long &val, bool typeshow = false) {
cout << "输入" << name;
if (typeshow) {
cout << "(为long long)";
}
cout << ":" ;
cin >> val;
return true;
}
bool input(string name, long &val, bool typeshow = false) {
cout << "输入" << name;
if (typeshow) {
cout << "(为long)";
}
cout << ":" ;
cin >> val;
return true;
}
bool input(string name, char &val, bool typeshow = false) {
cout << "输入" << name;
if (typeshow) {
cout << "(为char)";
}
cout << ":" ;
cin >> val;
return true;
}
//WORD:输入单词(空格结束)
//LINE:输入一行(回车结束)
enum {WORD, LINE};
bool input(string name, string &val, bool typeshow = false, int inputtype = WORD) {
cout << "输入" << name;
if (typeshow) {
cout << "(为string)";
}
cout << ":" ;
if (inputtype == WORD) {
cin >> val;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
//清空缓冲区 防止剩下的字符影响下一次读入
} else if (inputtype == LINE) {
getline(cin, val);
//读取一行
} else {
cout << "未识别的输入类型" << endl;
return false;
}
return true;
}
template <class T>
bool input(string name, vector<T> &val, int time, bool typeshow = false) {
try {
for (int i = 1; i <= time; i++) {
T temp;
if (!input(name + "的第" + to_string(i) + "项", temp, typeshow))
return false;
val.push_back(temp);
}
return true;
} catch (exception e) {
cout << e.what() << endl;
return false;
}
return false;
}
template <class K, class V>
bool input(string name, map<K, V> &val, int time, bool typeshow = false) {
try {
for (int i = 1; i <= time; i++) {
K k;
V v;
if (!input(name + "的第" + to_string(i) + "项键", k, typeshow))
return false;
if (!input(name + "的第" + to_string(i) + "项值", v, typeshow))
return false;
val[k] = v;
}
return true;
} catch (exception e) {
cout << e.what() << endl;
return false;
}
return false;
}
/* print:打印变量
参数:
name:要输出的变量名称提示
val:要输出的变量
返回:
bool:描述打印是否成功
目前支持的类型:
double
float
int
long
long long
string
char
vector<(前7个类型)>
map<(前7个类型),(前7个类型)>
*/
template <class T>
bool print(string name, T val) {
cout << "尝试输出print未定义的类型" << endl;
return false;
}
bool print(string name, int val) {
cout << name << ":" << val << endl;
return true;
}
bool print(string name, long val) {
cout << name << ":" << val << endl;
return true;
}
bool print(string name, double val) {
cout << name << ":" << val << endl;
return true;
}
bool print(string name, short val) {
cout << name << ":" << val << endl;
return true;
}
bool print(string name, float val) {
cout << name << ":" << val << endl;
return true;
}
bool print(string name, long long val) {
cout << name << ":" << val << endl;
return true;
}
bool print(string name, char val) {
cout << name << ":" << val << endl;
return true;
}
bool print(string name, string val) {
cout << name << ":" << val << endl;
return true;
}
template <class T>
bool print(string name, vector<T> &val) {
int i = 1;
for (auto v : val) {
if (!print(name + "第" + to_string(i) + "项", v))
return false;
i++;
}
return true;
}
template <class K, class V>
bool print(string name, map<K, V> &val) {
// 依靠版本
// for(auto [k,v] : val){
// if(!print(name+"的键",k)) return false;
// if(!print(name+"的值",v)) return false;
// }
// return true;
for (auto i = val.rbegin(); i != val.rend(); i++) {
if (!print(name + "的键", i->first)) return false;
if (!print(name + "的值", i->second)) return false;
}
return true;
}
#endif
text.cpp
测试用的 可以参照这个文件使用
#include <iostream>
#include "everything.h"
//导入头
int main() {
int num1;
input("num1",num1);//输入num1
print("num1 output",num1);//输出num1
string str1;
input("str1",str1,false,WORD);//输入一个单词
//第三个参数描述是否显示类型 (必须要有 因为我们要用第四个参数)
print("str1 output",str1);
string str2;
input("str2",str2,true,LINE);//输入一行
//第三个参数描述是否显示类型
print("str2 output",str2);
vector<float> vecf;
input("vector float",vecf,2);//向vecf中添加两个元素
print("vector float output",vecf);
map<char,int> map_c_i;
input("map char to int",map_c_i,2,true);//向map_c_i中添加两组键值并显示类型
print("map char to int output",map_c_i);
bitset<20> bt;
bool type = input("bitset",bt);//尝试输入一个未定义类型
if(type){
cout << "输入成功" << endl;
}else{
cout << "输入失败"<< endl;
}
print("bitset output",bt);//尝试输出
return 0;
}
3.效果
输入与输出
输入num1:1
num1 output:1
输入str1:2 3
str1 output:2
输入str2(为string):1 3
str2 output:1 3
输入vector float的第1项:1
输入vector float的第2项:2
vector float output第1项:1
vector float output第2项:2
输入map char to int的第1项键(为char):+
输入map char to int的第1项值(为int):1
输入map char to int的第2项键(为char):*
输入map char to int的第2项值(为int):2
map char to int output的键:+
map char to int output的值:1
map char to int output的键:*
map char to int output的值:2
尝试输入input未定义的类型
输入失败
尝试输出print未定义的类型
其中这种标记类型的文本为用户输入
截图
4.未来构想
将会添加对更多类的支持 直到只要对流运算符有重载就支持 理想情况下已经更新了下一篇 未来将持续更新