C++ STL IO流介绍

news2024/9/27 7:16:43

目录

一: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.coloradmin.cn/o/1914530.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

架构师机器学习操作 (MLOps) 指南

MLOps 是机器学习操作的缩写&#xff0c;是一组实践和工具&#xff0c;旨在满足工程师构建模型并将其投入生产的特定需求。一些组织从一些自主开发的工具开始&#xff0c;这些工具在每次实验后对数据集进行版本控制&#xff0c;并在每个训练周期后对检查点模型进行版本控制。另…

The First项目报告:引领L2解决方案新纪元的模块化协议AltLayer

在区块链演进中&#xff0c;可扩展性与定制化成为开发者核心诉求。ZK Rollups与Optimistic Rollups虽显著提升以太坊等区块链性能&#xff0c;却面临访问性、定制难、中心化风险及流动性分散等挑战。AltLayer以Rollups-as-a-Service创新模式&#xff0c;赋予开发者直接管理roll…

使用 MinIO 赢得 RAG 权利

人们常说&#xff0c;在人工智能时代&#xff0c;数据是你的护城河。为此&#xff0c;构建生产级 RAG 应用程序需要合适的数据基础架构来存储、版本控制、处理、评估和查询构成专有语料库的数据块。由于 MinIO 采用数据优先的 AI 方法&#xff0c;因此对于此类项目&#xff0c;…

前端面试代码题

1. 变量提升 2. 数组相关的方法 注意返回true值是保留不是过滤&#xff0c;别记反。 3. 引用相关 引用类型变量的等于号赋值&#xff0c;不会相互影响。但是通过a.b的方式会影响&#xff0c;此时算浅复制。 3. 宏任务微任务 2 4 5同步代码&#xff0c;3微任务&#xff0c;1宏…

【排序 - 直接选择排序】

选择排序&#xff08;Selection Sort&#xff09;是一种简单直观的排序算法&#xff0c;虽然不如快速排序或归并排序高效&#xff0c;但它易于理解和实现&#xff0c;适用于小型数据集合。 选择排序原理 选择排序的基本思想是每次从未排序的数据中选出最小&#xff08;或最大…

【Unity2D 2022:NPC】制作NPC

一、创建NPC角色 1. 创建JambiNPC并同时创建Jambi站立动画 &#xff08;1&#xff09;点击第一张图片&#xff0c;按住shift不松&#xff0c;再选中后两张图片&#xff0c;拖到层级面板中 &#xff08;2&#xff09;将动画资源文件保存到Animation Clips文件夹中 &#xff08;…

全栈业务开发入门——登录业务接口

业务已上传则资源 实现登录业务的前后端联调&#xff0c;前端点击登录按钮向后端发送一个请求&#xff0c;后端调用接口向前端响应结果 效果如下&#xff1a; 设计环境&#xff1a;springbootmybatisvue3axios 一.前端设计 1.基于vue3脚手架创建项目&#xff0c;搭建项目结构…

快问快答,一套源码的价格在多少钱左右?

购买一套商城源码的价格大致在几千元到几十万不等&#xff0c;具体取决于多种因素。下面将详细分析影响商城源码价格的主要因素&#xff1a; 商城类型 B2B2C商城&#xff1a;这类商城支持平台自营和多商户入驻&#xff0c;功能相对复杂&#xff0c;价格较高。一般价格在8-15万…

移动UI:发现页面,是什么,有啥作用,该如何设计呢?

移动应用中的“发现页面”通常用于向用户展示新的内容、功能、活动或推荐信息&#xff0c;以帮助用户发现和探索应用中的新内容或功能。设计一个好的发现页面可以提升用户对应用的兴趣和粘性。 以下是设计发现页面时可以考虑的一些要点&#xff1a; 1. 推荐内容&#xff1a; …

【网络安全科普】网络安全指南请查收

随着社会信息化深入发展&#xff0c;互联网对人类文明进步奖发挥更大的促进作用。但与此同时&#xff0c;互联网领域的问题也日益凸显。网络犯罪、网络监听、网络攻击等是又发生&#xff0c;网络安全与每个人都息息相关&#xff0c;下面&#xff0c;一起来了解网络安全知识吧。…

如何保障生物制药中试验网和办公网之间的跨网安全文件交换数据?

在针对数据化大环境下&#xff0c;生物制药企业的数据安全尤为关键&#xff0c;尤其是试验网与办公网之间的数据交换。这些数据不仅包含新药品研发成果、临床试验数据&#xff0c;还有健康医疗数据等&#xff0c;都是企业的核心竞争力和商业秘密 。因此&#xff0c;安全地进行跨…

代码随想录(day3)有序数组的平方

暴力求解法&#xff1a; 注意&#xff1a;需要确定范围&#xff0c;比如nums.sort()是在for循环之外&#xff0c;根据函数的功能来确定 return返回的是nums&#xff0c;而不是nums[i]因为返回的是整个数组 class Solution(object):def sortedSquares(self, nums):for i in r…

【Linux】:程序替换

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家解读一下有关Linux程序替换的相关知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门…

AWS-WAF-Log S3存放,通过Athena查看

1.创建好waf-cdn 并且设置好规则和log存储方式为s3 2. Amazon Athena 服务 使用 &#xff08;注意s3桶位置相同得区域&#xff09; https://docs.aws.amazon.com/zh_cn/athena/latest/ug/waf-logs.html#waf-example-count-matched-ip-addresses 官方文档参考,建一个分区查询表…

批量发送定制邮件内容

需要给以下学员发送作业反馈邮件&#xff08;文件名为&#xff1a;学员作业反馈.xlsx&#xff09; 学员序号学员姓名学员邮箱作业反馈20090001海龙3177261496qq.com第1题&#xff1a;少了一个a20090002真达3177261496qq.com第2题&#xff1a;少了一个b20090003张三3177261496…

Python: 分块读取文本文件

在处理大文件时&#xff0c;逐行或分块读取文件是很常见的需求。下面是几种常见的方法&#xff0c;用于在 Python 中分块读取文本文件&#xff1a; 1、问题背景 如何分块读取一个较大的文本文件&#xff0c;并提取出特定的信息&#xff1f; 问题描述: fopen(blank.txt,r) quot…

昇思25天学习打卡营第17天|应用实践之SSD目标检测

基本介绍 今天要学习的内容是计算机视觉领域中的目标检测任务。与图像分类相比&#xff0c;目标检测更难&#xff0c;因为目标检测不仅要检测出图片中的物体的类别&#xff0c;还要检测出该物体的位置。现主流的目标检测算法大致可分为两种&#xff0c;一种是基于CNN的&#xf…

Type-C/DP1.4到HDMI2.0替代龙讯LT8711HE,集睿智远CS5262

NCS8622是一款高性能低功耗的Type-C/DP1.4到HDMI2.0转换器&#xff0c;设计为连接USB Type-C源或DP1.4源到HDMI2.0。 NCS8622集成了符合DP1.4标准的接收器&#xff0c;以及符合HDMI2.0标准的发射器。此外&#xff0c;CC控制器用于将CC通信到实现DP Alt模式。DP接收器集成了HDCP…

软考五个高级科目怎么选?如何一口气拿下证书!

软考高级包括&#xff1a; 信息系统项目管理师、系统分析师、系统架构设计师、网络规划设计师、系统规划与管理师等五个考试。 一、各科特点&#xff1a; 信息系统项目管理师 特点&#xff1a;主要从事信息系统项目管理方面的工作&#xff0c;要求掌握项目管理的知识体系和实…

类与对象-继承-构造和析构顺序

构造和析构顺序 #include<iostream> using namespace std;class Base { public:Base(){cout << "Base构造函数" << endl;}~Base(){cout << "Base析构函数" << endl;} };class Son :public Base { public:Son(){cout <&l…