ca72a_c++_标准IO库:面向对象的标准库

/*ca72a_c++_标准IO库:面向对象的标准库
继承:基类-》派生类
3个头文件
9个标准库类型
IO对象不可复制或赋值

ofstream, f--file,文件输出流
ostringstream,o--输出,string字符串输出流
iostream--输入输出流

istringstream,i-输入,string字符串输入流
ifstream,i--输入,f--file,文件输入流

stringstream--字符串输入输出流, <sstream> ////字符串输入输出流头文件
fstream--文件输入输出流

 


*/

复制代码
 1 /*ca72a_c++_标准IO库:面向对象的标准库
 2 继承:基类-》派生类
 3 3个头文件
 4 9个标准库类型
 5 IO对象不可复制或赋值
 6 
 7 ofstream, f--file,文件输出流
 8 ostringstream,o--输出,string字符串输出流
 9 iostream--输入输出流
10 
11 istringstream,i-输入,string字符串输入流
12 ifstream,i--输入,f--file,文件输入流
13 
14 stringstream--字符串输入输出流, <sstream> ////字符串输入输出流头文件
15 fstream--文件输入输出流
16 
17 
18 
19 
20 */
21 
22 #include <iostream>
23 #include <fstream>
24 #include  <sstream> ////字符串输入输出流头文件
25 
26 using namespace std;
27 //void print(std::ofstream of)
28 //{
29 //    cout << "test!" << endl;
30 //}
31 void print(std::ofstream &of)//传递引用
32 {
33     cout << "ofstream test" << endl;
34 }
35 ofstream& print1(std::ofstream &of)//传递引用
36 {
37     cout << "ofstream test" << endl;
38     std::ofstream of2;
39     return of2;//返回引用是可以的, ofstream&
40 }
41 
42 void foo(ostream& os)
43 {
44     cout << "test ostream" << endl;
45     //os << "test ostream" << endl;
46 }
47 
48 int main()
49 {
50     //cout是ostream输出流对象
51     //cout都是在std名称空间里面。std::cout,std::endl;,std::cin
52     cout << "hello world!" << endl;
53     fstream fs;//文件输入输出流
54     //或者。std::fstream
55 
56     std::stringstream ss;//字符串输入输出流
57 
58     std::ofstream out1, out2;
59     //out1=out2;//错误。IO对象不可复制或赋值
60     //print(out1);//错误。IO对象不可复制或赋值,可以传参数与引用
61     print(out1);//可以传参数与引用
62 
63     //vector<ofstream> vec;//不能用容器
64 
65     foo(cout);//传递cout,cout是ostream
66 
67     cout << "ofstream test!!!" << endl;
68     std::ofstream ofs;
69     foo(ofs);// foo(ostream& os) osstream是基类,所以也可以接受它的派生类,ofstream
70 
71     ostringstream oss;
72     foo(oss);// foo(ostream& os) osstream是基类,所以也可以接受它的派生类,ostringstream
73     return 0;
74 }
复制代码

 

posted @   txwtech  阅读(187)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示