快速使用cpp的输入输出

  • 1.前言
    • 目的
    • 特点
    • 不足
  • 2.CV部分
    • everything.h
    • text.cpp
  • 3.效果
    • 输入与输出
    • 截图
  • 4.未来构想

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:描述输入是否有效目前支持的类型:doublefloatintlonglong longstringcharvector<(前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:描述打印是否成功目前支持的类型:doublefloatintlonglong longstringcharvector<(前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);//输入num1print("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.未来构想

将会添加对更多类的支持 直到只要对流运算符有重载就支持 理想情况下已经更新了下一篇 未来将持续更新

对c++输入输出的一些封装(bushi)相关推荐

  1. 【Go】Go基础(七):包

    一.标准库概述 1.官方手册 内置包在 Go 语言中有 150 个以上,它们被称为标准库,完整列表可以在Go Walker中查看 https://gowalker.org/search?q=gorep ...

  2. 软件工程结对项目:四则运算web

    1)Coding.Net项目地址 https://git.coding.net/DandelionClaw/WEB_Calculator.git 注:本项目为web端,并且需要连接SQL Server ...

  3. 50行实现C语言FM收音机-Taskbus Stdio封装器在SDR课程中的应用

    自从学校引入SDR教学以来,总觉得学生的实验课上的很吃力--还不如学习Matlab仿真算了.原因是编程基础不是很扎实的学生,可能只会使用C语言课上介绍的最基础的知识,这些知识往往无法支持其完成完整的S ...

  4. Go使用grpc+http打造高性能微服务

    大家可以发现,Go越来越流行,其一是目前云计算领域基本是使用Go作为底层开发语言:其二是随着区块链的火爆,引申出了其背后很多开源项目,很多都是使用Go语言进行开发:其三就是在微服务方面,Go也展示很大 ...

  5. 一篇让你熟练掌握Java常用工具包(全网最全)

    文章目录 Apache Commons类库 1 BeanUtils 2 Codec 3 Collections 4 I/O 4.1 工具类 4.2 尾端类 4.3 行迭代器 4.4 文件过滤器 4.5 ...

  6. Qualcomm QTV Player

    1 QTV Architecture QTV的高通的音视频解码方案,来自packetvideo的PV:Player.Architecture如下: (1)QCT Mediaplayer Applica ...

  7. Linux curses 总结一

    ps:以下内容可能有很多不正确的地方~ Curses是什么 Curses有什么用 Curses使用前注意事项 什么是stdsrc ,cursrc curses和stdsrc的关系 Curses第一个小 ...

  8. 仅需6步,教你轻易撕掉app开发框架的神秘面纱(6):各种公共方法及工具类的封装

    为什么要封装公共方法 封装公共方法有2方面的原因: 一是功能方面的原因:有些方法很多地方都会用,而且它输入输出明确,并且跟业务逻辑无关.比如检查用户是否登录,检查某串数字是否为合法的手机号.像这种方法 ...

  9. OSS.Core基于Dapper封装(表达式解析+Emit)仓储层的构思及实现

    最近趁着不忙,在构思一个搭建一个开源的完整项目,至于原因以及整个项目框架后边文章我再说明.既然要起一个完整的项目,那么数据仓储访问就必不可少,这篇文章我主要介绍这个新项目(OSS.Core)中我对仓储 ...

最新文章

  1. js上传文件,上传表单demo 包含后端php
  2. 基于B/S架构的故障模型
  3. jQuery左右循环滚动图片特效
  4. poj3278 CatchThatCow bfs
  5. 《如何搭建小微企业风控模型》第十三节 额度公式 节选
  6. android 7.1 支持哪些 cpu,锤子新机坚果Pro配置放出:骁龙626处理器、Android 7.1.1系统...
  7. android获取Mac地址和IP地址
  8. 最简单的文件加密工具(完全免费)
  9. 文档没保存可以找回吗?文件丢失恢复方法
  10. regedit是什么意思_regedit用法_regedit参数_reg的注册与反注册
  11. zblog导航小智收录网导航模板
  12. 希尔伯特几何基础序言
  13. 删除电脑被占用的串口
  14. PRML读书笔记(一)
  15. rap2搭建,mysql,redis,nginx安装,node环境安装,rap2安装
  16. php7新特性ppt,2019新版PPT,不知道这7个新功能,怎么做好幻灯片?
  17. Linux命令学习笔记(一)目录操作
  18. Shopee招聘主页下面一排建筑简笔画代表哪里
  19. 蓝桥云课linux入门2:基本概念及操作
  20. ECharts常用图例

热门文章

  1. Ubuntu高分屏下Matlab工具栏字体过小
  2. C语言预处理命令(预处理指令)
  3. 进程和程序区别和联系
  4. 维修服务器bga是什么,服务器主板芯片坏了有机器能拆除焊接BGA吗?
  5. 中断优先级和中断线程优先级
  6. 百度AI 实现人体姿态检测
  7. 实现ALOHA协议仿真算法
  8. 【考研高数-高等数学-基础】第二章 导数与微分
  9. Swing-文本的绘制(设置字体)
  10. sql中的dbl的含义