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

举报
小哈里 发表于 2022/05/11 00:07:37 2022/05/11
【摘要】 1、三个头文件 iostream: istream+ostream //读写流 #include<iostream> using namespace std; int main(){ i...

1、三个头文件

iostream: istream+ostream

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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;
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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;  //以十六进制形式输出整数a
	cout<<"oct:"<<setbase(8)<<a<<endl;  //以八进制形式输出整数a
	char *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;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3、<<重载

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

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

  
 
  • 1
  • 2
  • 3
  • 4

实现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;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/108399266

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。