深圳优化网站公司网络推广的公司是骗局吗
文章目录
- 1. C语言的输入与输出
- 2.C++的IO流
- 2.1流的概念
- 2.2C++IO流
- 2.3刷题常见while(cin >> str)
- 重载强制类型转换运算符
- 模拟while(cin >> str)
- 2.4C++标准IO流
- 2.5C++文件IO流
- 1.ifstream
1. C语言的输入与输出
C语言用到最频繁的输入输出方式就是scanf ()与printf()。
scanf(): 从标准输入设备(键盘)读取数据,并将值存放在变量中。
printf(): 将指定的文字/字符串输出到标准输出设备(屏幕)。
注意宽度输出和精度输出控制
对输入输出缓冲区的理解
1.可以屏蔽掉低级I/O的实现,低级I/O的实现依赖操作系统本身内核的实现,如果能够屏蔽这部分的差异,可以很容易写出可移植的程序。
2.可以使用这部分的内容实现“行”读取的行为,计算机没有“行”这个概念,有了这部分,可以定义“行”的概念,解析缓冲区的内容,返回一个“行”。
C/C++IO的区别与联系
ostream/istream 支持自定义类型对象的流插入和流提取[重载]
int main()
{//cin和scanf输入时 默认用空格/换行分割int year = 0, month = 0, day = 0;cin >> year >> month >> day;//scanf("%d%d%d", &year, &month, &day);//scanf("%d %d %d", &year, &month, &day); //也可以 没必要cout << year << month << day;//20231027scanf("%4d%2d%2d", &year, &month, &day);string str;cin >> str;year = stoi(str.substr(0, 4));month = stoi(str.substr(4, 2));day = stoi(str.substr(6, 2));cout << year << month << day;return 0;
}
2.C++的IO流
2.1流的概念
“流”即是流动的意思,物质从一处向另一处流动的过程,是对一种有序连续且具有方向性的数据( 单位可以是bit,byte,packet)的抽象描述。
C++流是指信息从外部输入设备(如键盘)向计算机内部(如内存)输入和从内存向外部输出设备(显示器)输出的过程。
这种输入输出的过程被形象的比喻为“流”。特性:有序连续、具有方向性
2.2C++IO流
2.3刷题常见while(cin >> str)
牛客/力扣常见: 多个测试样例
int main()
{int year, month, day;string str;while (cin >> str){year = stoi(str.substr(0, 4));month = stoi(str.substr(4, 2));day = stoi(str.substr(6, 2));cout << year << "年" << month << "月" << day << "日" << endl;}return 0;
}
为什么 cin >> str 能做为循环判断的条件?查阅文档如下
如果设置了故障位或坏位中的至少一个,则为空指针。其他一些值。
C++11 如果至少设置了其中一个错误标志(ctrl + Z +“+” 或者 ctrl + C[进程终止]),则函数返回false,否则返回true。
有些同学在这里看不懂operator bool是啥 建议看一下
重载强制类型转换运算符
重载强制类型转换运算符
class A
{
public:/*不存在从"int" 转换到"A"的适当构造函数explicit A(int _a):_a(_a){}*/A(int _a):_a(_a){}//如果不重载int 报错: A类型无法转换成intoperator int(){return _a;}private:int _a;
};int main()
{// 内置类型 转换成自定义类型// 隐式类型转换 // 用1构造A类型的临时对象 拷贝构造给aa1 // 优化后1直接去构造aa1A aa1 = 1; // 自定义类型 转换成内置类型int i = aa1; cout << i << endl;return 0;
}
模拟while(cin >> str)
class Date
{friend ostream& operator << (ostream& out, const Date& d);friend istream& operator >> (istream& in, Date& d);
public:Date(int year = 2023, int month = 10, int day = 27):_year(year), _month(month), _day(day){}explicit operator bool() const{//现在还没有2024年if (_year == 2024)return false;elsereturn true;}
private:int _year;int _month;int _day;
};istream& operator >> (istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}ostream& operator << (ostream& out, const Date& d)
{out << d._year << " " << d._month << " " << d._day;return out;
}int main()
{//cout 自动识别类型 -- 重载 流提取 运算符/*int b = 1;double j = 2.2;cout << b << endl;cout << j << endl;*/Date d(2023, 10, 27);cout << d << endl;//d是Date类型的 Date类重载了bool // 此处while循环判断的值是布尔类型 // 所以此处while(d) 相当于 while( d.operaotor bool() )//while(cin >> str) ==> operator>>(cin, str)[istream类里重载了bool强制类型转换运算符]while (d) {cin >> d;if(d)cout << "您输入的日期为: " << d << endl;elsecout << "输入错误 循环终止!" << endl;}return 0;
}
2.4C++标准IO流
C++标准库提供了4个全局流对象cin、cout、cerr、clog
使用cout进行标准输出,即数据从内存流向控制台(显示器)。
使用cin进行标准输入 , 即数据通过键盘输入到程序中,
cerr进行标准错误的输出
clog进行日志的输出
注意:
- cin为缓冲流。键盘输入的数据保存在缓冲区中,当要提取时,是从缓冲区中拿。如果一次输入过多,会留在那儿慢慢用,如果输入错了,必须在回车之前修改,如果回车键按下就无法挽回了。只有把输入缓冲区中的数据取完后,才要求输入新的数据。
- 输入的数据类型必须与要提取的数据类型一致,否则出错。出错只是在流的状态字state中对应位置位(置1),程序继续。
- 空格和回车都可以作为数据之间的分格符,所以多个数据可以在一行输入,也可以分行输入。但如果是字符型和字符串,则空格(ASCII码为32)无法用cin输入,字符串中也不能有空格。回车符也无法读入。
- cin和cout可以直接输入和输出内置类型数据,原因:标准库已经将所有内置类型的输入和输出全部重载了:
- 自定义类型如果要支持cin和cout的标准输入输出,需要对<<和>>进行重载。
- 连续输入时,vs系列编译器下在输入ctrl+Z时结束
2.5C++文件IO流
1.ifstream
输出文件
int main()
{ifstream ifs("test.cpp");char ch = ifs.get();while (ifs){cout << ch;ch = ifs.get();}return 0;
}
读文件
class Date
{friend ostream& operator << (ostream& out, const Date& d);friend istream& operator >> (istream& in, Date& d);
public:Date(int year = 2023, int month = 10, int day = 27):_year(year), _month(month), _day(day){}private:int _year;int _month;int _day;
};istream& operator >> (istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}ostream& operator << (ostream& out, const Date& d)
{out << d._year << " " << d._month << " " << d._day;return out;
}
int main()
{ifstream ifs("obj.txt");int i;string j;double k;Date d1;Date d2;Date d3;ifs >> i >> j >> k >> d1 >> d2 >> d3;cout << i << endl << j << endl << k << endl << d1 << endl << d2 << endl << d3 << endl;return 0;
}