C++ STL IO流介绍

目录

一:IO流的继承关系:

二:输入输出功能

1. 基本用法 

 2. 格式化输入

3.非格式化输入

4. 格式化输出

三:流

1. 字符流

2. 向字符流中写入数据

3. 从字符流中读出数据

4. 清空字符流

5.完整的例子

四:文件流


一:IO流的继承关系:

含义
basic_streambuf
 
读取或写入数据
ios_base独立于字符类型的流属性
basic_ios依赖于字符类型的流属性
basic_istream用于读取数据的流基类
basic_iostream用于写入数据的流基类
basic_iostream用于读写数据的流基类

二:输入输出功能

typedef basic_istream<char> istream;
typedef basic_ostream<char> ostream;
1. 基本用法 

#include <iostream>
int main(){
std::cout << "Type in your numbers";
std::cout << "(Quit with an arbitrary character): " << std::endl;
// 2000 <Enter> 11 <a>
int sum{0};
int val;
while (std::cin >> val) sum += val;
std::cout << "Sum: " << sum; // Sum: 2011
}
 2. 格式化输入
include <iostream>

int main()
{
    int a, b;
    std::cout << "Two natural numbers: " << std::endl;
    std::cin >> a >> b; // < 2000 11>
    std::cout << "a: " << a << " b: " << b;
}
3.非格式化输入
#include <iostream>

int main()
{
    std::string line;
    std::cout << "Write a line: " << std::endl;
    std::getline(std::cin, line); // <Only for testing purpose.>
    std::cout << line << std::endl; // Only for testing purpose.
    std::cout << "Write numbers, separated by;" << std::endl;
    while (std::getline(std::cin, line, ';') ) 
    {
        std::cout << line << " ";
    } 
}
4. 格式化输出
#include <iostream>

int main()
{
    int num{2011};
    std::cout.setf(std::ios::hex, std::ios::basefield);
    std::cout << num << std::endl; // 7db
    std::cout.setf(std::ios::dec, std::ios::basefield);
    std::cout << num << std::endl; // 2011
    std::cout << std::hex << num << std::endl; // 7db
    std::cout << std::dec << num << std::endl; // 2011
}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>

int main()
{


	std::cout.fill('#');
	std::cout << -12345;
	std::cout << std::setw(10) << -12345; // ####-12345
	std::cout << std::setw(10) << std::left << -12345; // -12345####
	std::cout << std::setw(10) << std::right << -12345; // ####-12345
	std::cout << std::setw(10) << std::internal << -12345; //-####12345
	std::cout << std::oct << 2011; // 3733
	std::cout << std::hex << 2011; // 7db
	std::cout << std::showbase;
	std::cout << std::dec << 2011; // 2011
	std::cout << std::oct << 2011; // 03733
	std::cout << std::hex << 2011; // 0x7db
	std::cout << 123.456789; // 123.457
	std::cout << std::fixed;
	std::cout << std::setprecision(3) << 123.456789; // 123.457
	std::cout << std::setprecision(6) << 123.456789; // 123.456789
	std::cout << std::setprecision(9) << 123.456789; // 123.456789000
	std::cout << std::scientific;
	std::cout << std::setprecision(3) << 123.456789; // 1.235e+02
	std::cout << std::setprecision(6) << 123.456789; // 1.234568e+02
	std::cout << std::setprecision(9) << 123.456789; // 1.234567890e+02
	std::cout << std::hexfloat;
	std::cout << std::setprecision(3) << 123.456789; // 0x1.edd3c07ee0b0bp+6
	std::cout << std::setprecision(6) << 123.456789; // 0x1.edd3c07ee0b0bp+6
	std::cout << std::setprecision(9) << 123.456789; // 0x1.edd3c07ee0b0bp+6
	std::cout << std::defaultfloat;
	std::cout << std::setprecision(3) << 123.456789; // 123
	std::cout << std::setprecision(6) << 123.456789; // 123.457
	std::cout << std::setprecision(9) << 123.456789; // 123.456789

}

三:流

1. 字符流
//String stream for the input of data of type char and wchar_t.
std::istringstream and std::wistringstream

//String stream for the output of data of type char and wchar_t.
std::ostringstream and std::wostringstream

//String stream for the input or output of data of type char and wchar_t.
std::stringstream and std::wstringstream
2. 向字符流中写入数据
std::stringstream os;
os << "New String";
os.str("Another new String");
3. 从字符流中读出数据
std::string os;
std::string str;
os >> str;
str= os.str();
4. 清空字符流
std::stringstream os;
os.str("");
5.完整的例子
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>

template <typename T>
T StringTo(const std::string& source) {
	std::istringstream iss(source);
	T ret;
	iss >> ret;
	return ret;
}

template <typename T>
std::string ToString(const T& n) {
	std::ostringstream tmp;
	tmp << n;
	return tmp.str();
}

int main()
{
	std::cout << "5= " << StringTo<int>("5"); // 5
	std::cout << "5 + 6= " << StringTo<int>("5") + 6; // 11
	std::cout << ToString(StringTo<int>("5") + 6); // "11"
	std::cout << "5e10: " << std::fixed << StringTo<double>("5e10"); // 50000000000
}

四:文件流

//File stream for the input of data of type char and wchar_t.
std::ifstream and std::wifstream

//File stream for the output of data of type char and wchar_t.
std::ofstream and std::wofstream

//File stream for the input and output of data of type char and wchar_t.
std::fstream and std::wfstream

//Data buffer of type char and wchar_t.
std::filebuf and std::wfilebuf
#include <fstream>

int main()
{
    std::ifstream in("inFile.txt");
    std::ofstream out("outFile.txt");
    out << in.rdbuf();
}
#include <fstream>
#include <iostream>
#include <istream>
#include <string>

void writeFile(const std::string name) {
	std::ofstream outFile(name);
	if (!outFile) {
		std::cerr << "Could not open file " << name << std::endl;
		exit(1);
	}
	for (unsigned int i = 0; i < 10; ++i) {
		outFile << i << " 0123456789" << std::endl;
	}
}

int main()
{
	std::string random{ "random.txt" };
	writeFile(random);
	std::ifstream inFile(random);
	if (!inFile) {
		std::cerr << "Could not open file " << random << std::endl;
		exit(1);
	}
	std::string line;
	std::cout << inFile.rdbuf();
	// 0 0123456789
	// 1 0123456789

		// 9 0123456789
	std::cout << inFile.tellg() << std::endl; // 200
	inFile.seekg(0); // inFile.seekg(0, std::ios::beg);
	std::getline(inFile, line);

	std::cout << line; // 0 0123456789
	inFile.seekg(20, std::ios::cur);
	std::getline(inFile, line);
	std::cout << line; // 2 0123456789
	inFile.seekg(-20, std::ios::end);
	std::getline(inFile, line);
	std::cout << line; // 9 0123456789
}

五:IO流运算符重载,支持用户自定义类型输入输出

friend std::istream& operator>> (std::istream& in, Fraction& frac);
friend std::ostream& operator<< (std::ostream& out, const Fraction& frac);
#include <fstream>
#include <iostream>
#include <istream>
#include <string>


class Fraction {
public:
	Fraction(int num = 0, int denom = 0) :numerator(num), denominator(denom) {}
	friend std::istream& operator>> (std::istream& in, Fraction& frac);
	friend std::ostream& operator<< (std::ostream& out, const Fraction& frac);
private:
	int numerator;
	int denominator;
};
std::istream& operator>> (std::istream& in, Fraction& frac) {
	in >> frac.numerator;
	in >> frac.denominator;
	return in;
}
std::ostream& operator<< (std::ostream& out, const Fraction& frac) {
	out << frac.numerator << "/" << frac.denominator;
	return out;
}

int main()
{
	Fraction frac(3, 4);
	std::cout << frac; // 3/4
	std::cout << "Enter two numbers: ";
	Fraction fracDef;
	std::cin >> fracDef; // <1 2>
	std::cout << fracDef; // 1/2

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/790479.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

RISC-V异常处理流程概述(2):异常处理机制

RISC-V异常处理流程概述(2):异常处理机制 一、异常处理流程和异常委托1.1 异常处理流程1.2 异常委托二、RISC-V异常处理中软件相关内容2.1 异常处理准备工作2.2 异常处理函数2.3 Opensbi系统调用的注册一、异常处理流程和异常委托 1.1 异常处理流程 发生异常时,首先需要执…

生物打印后的生物力学过程

生物打印后的生物力学过程 3D生物打印技术在组织工程领域展现出巨大的潜力&#xff0c;但打印后组织的生物力学特性对其最终成功至关重要。本文将详细介绍打印后组织的生物力学特性及其在组织工程中的应用。 1. 打印后水凝胶交联 原位交联可以在生物打印过程中提供足够的机械…

开发个人Go-ChatGPT--5 模型管理 (一)

开发个人Go-ChatGPT–5 模型管理 (一) 背景 开发一个chatGPT的网站&#xff0c;后端服务如何实现与大模型的对话&#xff1f;是整个项目中开发困难较大的点。 如何实现上图的聊天对话功能&#xff1f;在开发后端的时候&#xff0c;如何实现stream的响应呢&#xff1f;本文就…

SprintBoot创建遇到的问题

最近使用IDEA版本为2022.3.1&#xff0c;java版本为21.0.3&#xff0c;现在做一个创建SprintBoot3的一个大体流程 1.先下载Maven&#xff0c;解压到一个位置 maven下载 2.配置setting.xml文件 这路径自己配置&#xff0c;这里不多演示 代码如下&#xff1a; <mirror>&…

开源网页终端webssh容器镜像制作与使用

1.Dockerfile编写&#xff1a; # 指定镜像目标平台与镜像名 alpine表示基础镜像 第一层镜像 FROM --platform$TARGETPLATFORM alpine # 添加元数据到镜像 LABEL maintainer"Jrohy <euvkzxgmail.com>" # 编译时变量 ARG TARGETARCH # 执行编译命令&#xff0c;…

代码随想录算法训练营第四十九天| 647. 回文子串、 516.最长回文子序列

647. 回文子串 题目链接&#xff1a;647. 回文子串 文档讲解&#xff1a;代码随想录 状态&#xff1a;不会 思路&#xff1a; dp[i][j] 表示字符串 s 从索引 i 到索引 j 这一段子串是否为回文子串。 当s[i]与s[j]不相等&#xff0c;那没啥好说的了&#xff0c;dp[i][j]一定是fa…

柔性测斜仪:监测钻孔位移的核心利器

柔性测斜仪&#xff0c;作为一款创新的测量工具&#xff0c;凭借其卓越的设计与性能&#xff0c;在地下建筑、桥梁、隧道及水利水电工程等领域展现出非凡的应用价值。其安装便捷、操作简便、高精度及长寿命等特性&#xff0c;使之成为监测钻孔垂直与水平位移的理想选择。以下是…

打卡第8天-----字符串

进入字符串章节了,我真的特别希望把leetcode上的题快点全部都给刷完,我是社招准备跳槽才选择这个训练营的,面试总是挂算法题和编程题,希望通过这个训练营我的算法和编程的水平能有所提升,抓住机会,成功上岸。我现在的这份工作,真的是一天都不想干了,但是下家工作单位还…

VS Code 扩展如何发布到私有Nexus的正确姿势

VS Code扩展的发布 VS Code 扩展的发布需要使用到vsce&#xff0c;vsce是一个用于打包、发布和管理 VS Code 扩展的命令行工具。可以通过 npm 来全局安装它&#xff1a; npm install -g vsce发布扩展到微软的应用市场 VS Code 的应用市场基于微软自己的 Azure DevOps。要发布…

计算机网络--tcpdump和iptable设置、内核参数优化策略

tcpdump工具 tcpdump命令&#xff1a; 选项字段&#xff1a; 过滤表达式&#xff1a; 实用命令&#xff1a; TCP三次握手抓包命令&#xff1a; #客户端执行tcpdump 抓取数据包 tcpdump -i etho tcp and host 192.168.12.36 and port 80 -W timeout.pcapnetstat命令 netst…

element el-table实现表格动态增加/删除/编辑表格行,带校验规则

本篇文章记录el-table增加一行可编辑的数据列&#xff0c;进行增删改。 1.增加空白行 直接在页面mounted时对form里面的table列表增加一行数据&#xff0c;直接使用push() 方法增加一列数据这个时候也可以设置一些默认值。比如案例里面的 产品件数 。 mounted() {this.$nextTi…

[嵌入式 C 语言] 按位与、或、取反、异或

若协议中如下图所示&#xff1a; 注意&#xff1a; 长度为1&#xff0c;表示1个字节&#xff0c;也就是0xFF&#xff0c;也就是 1111 1111 &#xff08;这里0xFF只是单纯表示一个数&#xff0c;也可以是其他数&#xff0c;这里需要注意的是1个字节的意思&#xff09; 一、按位…

URI:URL、URN

名称解释&#xff1a; URI:统一资源标识符&#xff1b; URL:统一资源定位符; URN:统一资源命名符&#xff1b; URI、URL、URN关系 URI是URL和URN的超集,也就是说URI有两种方式&#xff0c;一种是URL一种是URN,不过URL的方式用的比较多。 看了一个视频&#xff0c;博主解释非…

xcode配置swift使用自定义主题颜色或者使用RGB或者HEX颜色

要想在xcode中使用自定义颜色或者配置主题色&#xff0c;需要在Assets中配置&#xff0c;打开Assets文件&#xff0c;然后点击添加Color Set&#xff1a; 输入颜色的名称&#xff0c;然后选中这个颜色&#xff0c;会出现两个颜色&#xff1a; Any Appearance表示亮色模式下使用…

基于uni-app与图鸟UI的知识付费小程序模板

一、项目概述 在知识经济蓬勃发展的背景下&#xff0c;移动互联网成为知识传播与消费的重要渠道。本项目旨在利用前沿的前端技术栈——uni-app及高效UI框架图鸟UI&#xff0c;打造一款集多功能于一体的、面向广大求知者的知识付费平台移动端模板。该模板旨在简化开发流程&…

【最强八股文 -- 计算机网络】【快速版】TCP 与 UDP 头部格式

目标端口和源端口: 应该把报文发给哪个进程包长度: UDP 首部的长度跟数据的长度之和校验和: 为了提供可靠的 UDP 首部和数据而设计&#xff0c;接收方使用检验和来检查该报文段中是否出现差错 源端口号和目的端口号: 用于多路复用/分解来自或送到上层应用的数据。告诉主机报文段…

轻松理解c++17的string_view

文章目录 轻松理解c17的string_view设计初衷常见用法构造 std::string_view常用操作作为函数参数 注意事项总结 轻松理解c17的string_view std::string_view 是 C17 引入的一个轻量级、不拥有&#xff08;non-owning&#xff09;的字符串视图类。它的设计初衷是提供一种高效、…

Web学习day03

maven&Mybatis 目录 maven&Mybatis 文章目录 一、maven 1.1作用 1.2仓库 1.3命令 1.4依赖范围 1.5生命周期 二、MyBatis 2.1简介 2.2API 2.3增删改的实现&案例 总结 一、maven 1.1作用 统一项目结构&#xff1b;项目构建&#xff1a;通过简单命令&a…

高阶面试-dubbo的学习

SPI机制 SPI&#xff0c;service provider interface&#xff0c;服务发现机制&#xff0c;其实就是把接口实现类的全限定名配置在文件里面&#xff0c;然后通过加载器ServiceLoader去读取配置加载实现类&#xff0c;比如说数据库驱动&#xff0c;我们把mysql的jar包放到项目的…

16. Revit API: Family、FamilySymbol、FamilyInstance

前言 前面写着一直絮絮叨叨&#xff0c;感觉不好。想找些表情包来&#xff0c;写得好玩点&#xff0c;但找不到合适的&#xff0c;或者说耗时费力又不满意&#xff0c;而自个儿又做不来表情包&#xff0c;就算了。 其次呢&#xff0c;之前会把部分类成员给抄表列出来&#xf…