1、三个头文件

iostream: istream+ostream

//读写流
#include<iostream>
using namespace std;
int main(){int a, b;cin>>a>>b;cout<<a+b<<endl;return 0;
}

fstream:ifstream+ofstream

//读写文件
#include<fstream>
using namespace std;
int main(){int a, b, c;ifstream in1;  in1.open("input.txt");ofstream out1;  out1.open("output.txt", ofstream::app);in1>>a>>b;out1<<a+b<<endl;return 0;
}

sstream=istringstream+ostringstream

//读写字符串
#include<iostream>
#include<sstream>
using namespace std;
int main(){string a = "abcde";istringstream st1; //定义输入流st1.str(a); //拷贝内容到流中string b = st1.str();//返回流中内容string c;  st1>>c;//输出内容到流中ostringstream st2; //定义输出流st2<<a<<"12345";  //输出内容到流中string d = st2.str(); //返回流中内容cout<<b<<" "<<c<<" "<<d<<endl;return 0;
}

2、输出格式

#include<iostream>
#include<iomanip>//不要忘记包含此头文件
using namespace std;
int main(){int a;cout<<"input a:";cin>>a;cout<<"dec:"<<dec<<a<<endl;  //以十进制形式输出整数cout<<"hex:"<<hex<<a<<endl;  //以十六进制形式输出整数acout<<"oct:"<<setbase(8)<<a<<endl;  //以八进制形式输出整数achar *pt="China";  //pt指向字符串"China"cout<<setw(10)<<pt<<endl;  //指定域宽为,输出字符串cout<<setfill('*')<<setw(10)<<pt<<endl;  //指定域宽,输出字符串,空白处以'*'填充double pi=22.0/7.0;  //计算pi值//按指数形式输出,8位小数cout<<setiosflags(ios::scientific)<<setprecision(8);cout<<"pi="<<pi<<endl;  //输出pi值cout<<"pi="<<setprecision(4)<<pi<<endl;  //改为位小数cout<<"pi="<<setiosflags(ios::fixed)<<pi<<endl;  //改为小数形式输出return 0;
}

3、<<重载

>>运算符左侧为流输入对象,右侧为结构对象,返回一个流输入对象。
注意引用符号&

istream& operator>>(istream& in, complex& A){in >> A.m_real >> A.m_imag;return in;
}

实现pair,complex类型。

#include <iostream>
using namespace std;class complex{public:complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
public:friend complex operator+(const complex & A, const complex & B);friend complex operator-(const complex & A, const complex & B);friend complex operator*(const complex & A, const complex & B);friend complex operator/(const complex & A, const complex & B);friend istream & operator>>(istream & in, complex & A);friend ostream & operator<<(ostream & out, complex & A);
private:double m_real;  //实部double m_imag;  //虚部
};//重载加法运算符
complex operator+(const complex & A, const complex &B){complex C;C.m_real = A.m_real + B.m_real;C.m_imag = A.m_imag + B.m_imag;return C;
}//重载减法运算符
complex operator-(const complex & A, const complex &B){complex C;C.m_real = A.m_real - B.m_real;C.m_imag = A.m_imag - B.m_imag;return C;
}//重载乘法运算符
complex operator*(const complex & A, const complex &B){complex C;C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag;C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag;return C;
}//重载除法运算符
complex operator/(const complex & A, const complex & B){complex C;double square = A.m_real * A.m_real + A.m_imag * A.m_imag;C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square;C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square;return C;
}//重载输入运算符
istream& operator>>(istream& in, complex& A){in >> A.m_real >> A.m_imag;return in;
}//重载输出运算符
ostream& operator<<(ostream& out, complex& A){out << A.m_real <<" + "<< A.m_imag <<" i ";;return out;
}int main(){complex c1, c2, c3;cin>>c1>>c2;c3 = c1 + c2;cout<<"c1 + c2 = "<<c3<<endl;c3 = c1 - c2;cout<<"c1 - c2 = "<<c3<<endl;c3 = c1 * c2;cout<<"c1 * c2 = "<<c3<<endl;c3 = c1 / c2;cout<<"c1 / c2 = "<<c3<<endl;return 0;
}

C++ IO库:cmd读写,字符串读写,文件读写,<<重载,标准输出相关推荐

  1. Objective-C 【从文件中读写字符串(直接读写/通过NSURL读写)】

    ------------------------------------------- 从文件中读写字符串(直接读写/通过NSURL读写) #import <Foundation/Foundat ...

  2. 【Python 基础教程 20】全面掌握Python3输入输出:从入门到高级的实用指南(文件读写,Excel读写,Markdowm文件读写)

    目录标题 1. 简介 1.1 Python3 输入输出的定义 1.2 输入输出的重要性和常见应用场景 2. 标准输入输出 2.1 介绍标准输入(stdin) 2.2 介绍标准输出(stdout) 2. ...

  3. python文件读写r+_python文件读写操作(r/r+/rb/w/w+/wb/a/a+/ab)

    一.常用 'r':只读.该文件必须已存在. 'r+':可读可写.该文件必须已存在,写为追加在文件内容末尾. 'rb':表示以二进制方式读取文件.该文件必须已存在. 'w':只写.打开即默认创建一个新文 ...

  4. python对文件的读写操作方式-python 文件读写操作

    读文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): >>> f = open('test.txt', 'r') r表示是文本文件,rb是二进制文件 ...

  5. python文件读写_python基础-文件读写'r' 和 'rb'区别

    一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据. 'w':表示要向文件写入数据, ...

  6. Python文件读写缓冲区和文件读写操作实例

    1.python文件缓冲区 f = open('test', 'w', 1) # 第三个参数为1表示行缓冲,默认值为-1 while True:data = input('>>>') ...

  7. python 文件读写 a+_python文件读写

    一.read. open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, o ...

  8. c# 实现二进制文件读写、ini文件读写以及一些文件和目录的常用操作,在某些特定情况下会用到,如录波文件要保存.dat

    不说废话,直接上代码 还包含一些常用的目录和文件操作的函数,如获得文件大小,获得文件行数,获得文件时间,获得目录下所有的文件名称等等等

  9. python文件读写到list_Python文件读写

    文件处理 f= file('poem.txt', 'w') # open for 'w'riting模式可以为读模式('r').写模式('w')或追加模式('a') f.write(poem) # w ...

  10. pythonfopen_Python之文件读写详解

    本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 I/O在计算机中是指Input ...

最新文章

  1. python任意输入一个正整数、判断该数是否为素数_Python编程判断一个正整数是否为素数的方法,python素数...
  2. 必须要改变这样的生活
  3. Leaflet中使用leaflet.easyPrint插件实现打印效果
  4. PHP生成静态HTML的源代码及用法
  5. 你需要知道的nginx304
  6. socket,TCP/IP的理解(转)
  7. 抽屉远离在计算机的应用,抽屉原理的应用及其推广优秀毕业论文
  8. Java中的状态设计模式
  9. 面试提问vue中v-if与v-show的区别以及使用场景
  10. 关于微型计算机的原理 叙述正确的是,微型计算机原理练习附答案概念.doc
  11. Java 找到并返回一组字符串中第一个不为空的字符串
  12. 数字钟实验报告stm32_单片机-电子时钟实验报告
  13. 第 4 章 程序计数器
  14. 王小毛是懒惰了,还是堕落了?
  15. linux系统root用户忘记密码的重置方法
  16. [DELPHI] 使用mod函数换行
  17. 【mosek.fusion】Primal SVM
  18. excel随机抽取一个数据,抽奖
  19. python分割图片、合并图片
  20. 组态王——创建协议组件失败

热门文章

  1. matplotlib 可视化 —— 定制 matplotlib
  2. C Tricks(十六)—— 复制字符串
  3. 破解数字游戏 —— 概率篇
  4. 【笔试/面试】数组及其内存结构
  5. 辨异 —— Python 的深拷贝与浅拷贝
  6. 高大上的集团名字_中国办公家具行业“高大上”的企业文化节 非中泰龙集团莫属!...
  7. c语言以空格分割字符串_如何统计字符串中单词的个数?
  8. 一张图学会python3高清图-一张图理清 Python3 所有知识点
  9. python读取指定路径txt文件-如何使用pandas读取txt文件中指定的列(有无标题)
  10. python是干嘛的-python语言是干什么的