跳转至内容
  • 版块
  • 最新
  • 标签
  • 热门
  • 用户
  • 群组
  • 搜索
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(不使用皮肤)
  • 不使用皮肤
折叠

Set☆Fire

  1. 主页
  2. Software
  3. C++中流重载使用方法

C++中流重载使用方法

已定时 已固定 已锁定 已移动 Software
7 帖子 1 发布者 5.4k 浏览 1 关注中
  • 从旧到新
  • 从新到旧
  • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • RNAtlance203R 离线
    RNAtlance203R 离线
    RNAtlance203
    写于 最后由 RNAtlance203 编辑
    #1

    codelist_0.cpp

    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<string>
    #include<stdlib.h>
    #include<set>
    
    using namespace std;
    class type
    {
    private:
    	int str1;
    	int str2;
    	int str3;
    	
    public:
    	type() {
    		str1 = 0, str2 = 0, str3 = 0;
    	}
    	~type() {};
    	friend istream & operator>>(istream & in, type &cu ) {
    		in >> cu.str1 >> cu.str2 >> cu.str3;
    		return in;
    	}
    	friend ostream & operator<<(ostream & out, const type &st) {
    		out << st.str1 << st.str2 << st.str3 << endl;
    		return out;
    	}
    };
    
    int main() {
    	type type0;
    	cin >> type0;
    	cout << type0;
    }
    

    这应该是最简单的重载流示例了

    1 条回复 最后回复
    0
    • RNAtlance203R 离线
      RNAtlance203R 离线
      RNAtlance203
      写于 最后由 RNAtlance203 编辑
      #2

      那么有什么实用价值呢

      如下
      codelist_1.cpp

      // codelist_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
      //
      #include<iostream>
      #include<iomanip>
      #include<fstream>
      #include<string>
      #include<stdlib.h>
      #include<set>
      
      using namespace std;
      class type
      {
      private:
      	int str1;
      	int str2;
      	int str3;
      	
      public:
      	type() {
      		str1 = 0, str2 = 0, str3 = 0;
      	}
      	~type() {};
      	friend istream & operator>>(istream & in, type &cu ) {
      		in >> cu.str1 >> cu.str2 >> cu.str3;
      		return in;
      	}
      	friend ostream & operator<<(ostream & out, const type &st) {
      		out << st.str1 << st.str2 << st.str3 << endl;
      		return out;
      	}
      	int getstr1() {
      		return str1;
      	}
      	int getstr2() {
      		return str2;
      	}
      	int getstr3() {
      		return str3;
      	}
      };
      
      int main() {
      	type type0;
      	ofstream fkb("fkb.txt");
      	cin >> type0;
      	fkb << type0.getstr1() << type0.getstr2() << type0.getstr3() << endl;
      	return 0;
      }
      

      实际作用大概是省下了从键盘cin到文件整个过程的文字量,而且这份代码是示例,class只有3个私有成员,如果是大型工程,一个一个cin写那必然是要出人命的

      1 条回复 最后回复
      0
      • RNAtlance203R 离线
        RNAtlance203R 离线
        RNAtlance203
        写于 最后由 RNAtlance203 编辑
        #3

        还可以拓展一下 虽然用了臭名昭著的goto,(但是)对于这种规模的东西也没必要上那些高端操作其实就是偷懒
        codelist_2.cpp

        // codelist_2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
        //
        #include<iostream>
        #include<iomanip>
        #include<fstream>
        #include<string>
        #include<stdlib.h>
        #include<set>
        
        using namespace std;
        class type
        {
        protected:
        	int str1;
        	int str2;
        	int str3;
        	
        public:
        	type() {
        		str1 = 0, str2 = 0, str3 = 0;
        	}
        	~type() {};
        	friend istream & operator>>(istream & in, type &cu ) {
        		in >> cu.str1 >> cu.str2 >> cu.str3;
        		return in;
        	}
        	friend ostream & operator<<(ostream & out, const type &st) {
        		out << st.str1 << st.str2 << st.str3 << endl;
        		return out;
        	}
        	int getstr1() {
        		return str1;
        	}
        	int getstr2() {
        		return str2;
        	}
        	int getstr3() {
        		return str3;
        	}
        };
        
        void readFile() {
        	type type0;
        	ifstream fkb("fkb.txt");
        	fkb >> type0;
        	cout << type0 << endl;
        	fkb.close();
        }
        
        void writeFile() {
        	type type0;
        	ofstream fkb("fkb.txt");
        	cin >> type0;
        	fkb << type0 << endl;
        	fkb.close();
        }
        
        
        
        int main() {
        	int choice;
        MAIN: {
        	system("cls");
        	cout << "1=read/2=write/0=exit" << endl;
        	cin >> choice;
        	if (choice == 1) goto READ;
        	else
        	{
        		if (choice == 2) goto WRITE;
        		else return 0;
        		}
        	}
        
        READ: {
        	  readFile();
        	  system("pause");
        	  goto MAIN;
        	}
        
        WRITE:{
        	  writeFile();
        	  system("pause");
        	  goto MAIN;
        	}
        	return 0;
        }
        
        RNAtlance203R 1 条回复 最后回复
        0
        • RNAtlance203R 离线
          RNAtlance203R 离线
          RNAtlance203
          写于 最后由 编辑
          #4

          还有个问题,就是如果私有或者受保护成员是字符串或者数组的话怎么整
          (史前大坑)

          RNAtlance203R 1 条回复 最后回复
          0
          • RNAtlance203R 离线
            RNAtlance203R 离线
            RNAtlance203
            写于 最后由 编辑
            #5

            还有,我是不写注释人,自己的代码只要自己看得懂即可

            1 条回复 最后回复
            0
            • RNAtlance203R RNAtlance203

              还可以拓展一下 虽然用了臭名昭著的goto,(但是)对于这种规模的东西也没必要上那些高端操作其实就是偷懒
              codelist_2.cpp

              // codelist_2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
              //
              #include<iostream>
              #include<iomanip>
              #include<fstream>
              #include<string>
              #include<stdlib.h>
              #include<set>
              
              using namespace std;
              class type
              {
              protected:
              	int str1;
              	int str2;
              	int str3;
              	
              public:
              	type() {
              		str1 = 0, str2 = 0, str3 = 0;
              	}
              	~type() {};
              	friend istream & operator>>(istream & in, type &cu ) {
              		in >> cu.str1 >> cu.str2 >> cu.str3;
              		return in;
              	}
              	friend ostream & operator<<(ostream & out, const type &st) {
              		out << st.str1 << st.str2 << st.str3 << endl;
              		return out;
              	}
              	int getstr1() {
              		return str1;
              	}
              	int getstr2() {
              		return str2;
              	}
              	int getstr3() {
              		return str3;
              	}
              };
              
              void readFile() {
              	type type0;
              	ifstream fkb("fkb.txt");
              	fkb >> type0;
              	cout << type0 << endl;
              	fkb.close();
              }
              
              void writeFile() {
              	type type0;
              	ofstream fkb("fkb.txt");
              	cin >> type0;
              	fkb << type0 << endl;
              	fkb.close();
              }
              
              
              
              int main() {
              	int choice;
              MAIN: {
              	system("cls");
              	cout << "1=read/2=write/0=exit" << endl;
              	cin >> choice;
              	if (choice == 1) goto READ;
              	else
              	{
              		if (choice == 2) goto WRITE;
              		else return 0;
              		}
              	}
              
              READ: {
              	  readFile();
              	  system("pause");
              	  goto MAIN;
              	}
              
              WRITE:{
              	  writeFile();
              	  system("pause");
              	  goto MAIN;
              	}
              	return 0;
              }
              
              RNAtlance203R 离线
              RNAtlance203R 离线
              RNAtlance203
              写于 最后由 RNAtlance203 编辑
              #6

              @rnatlance203 在 C++中流重载使用方法 中说:

              还可以拓展一下 虽然用了臭名昭著的goto,(但是)对于这种规模的东西也没必要上那些高端操作其实就是偷懒
              codelist_2.cpp

              // codelist_2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
              //
              #include<iostream>
              #include<iomanip>
              #include<fstream>
              #include<string>
              #include<stdlib.h>
              #include<set>
              
              using namespace std;
              class type
              {
              protected:
              	int str1;
              	int str2;
              	int str3;
              	
              public:
              	type() {
              		str1 = 0, str2 = 0, str3 = 0;
              	}
              	~type() {};
              	friend istream & operator>>(istream & in, type &cu ) {
              		in >> cu.str1 >> cu.str2 >> cu.str3;
              		return in;
              	}
              	friend ostream & operator<<(ostream & out, const type &st) {
              		out << st.str1 << st.str2 << st.str3 << endl;
              		return out;
              	}
              	int getstr1() {
              		return str1;
              	}
              	int getstr2() {
              		return str2;
              	}
              	int getstr3() {
              		return str3;
              	}
              };
              
              void readFile() {
              	type type0;
              	ifstream fkb("fkb.txt");
              	fkb >> type0;
              	cout << type0 << endl;
              	fkb.close();
              }
              
              void writeFile() {
              	type type0;
              	ofstream fkb("fkb.txt");
              	cin >> type0;
              	fkb << type0 << endl;
              	fkb.close();
              }
              
              
              
              int main() {
              	int choice;
              MAIN: {
              	system("cls");
              	cout << "1=read/2=write/0=exit" << endl;
              	cin >> choice;
              	if (choice == 1) goto READ;
              	else
              	{
              		if (choice == 2) goto WRITE;
              		else return 0;
              		}
              	}
              
              READ: {
              	  readFile();
              	  system("pause");
              	  goto MAIN;
              	}
              
              WRITE:{
              	  writeFile();
              	  system("pause");
              	  goto MAIN;
              	}
              	return 0;
              }
              

              我是不知道我拓展了个什么玩意,我只知道大半夜很容易出错

              函数void readFile()只能读第一行而且会把所有数据连在一起,一眼就能看出;
              函数void writeFile()写入的时候会把所有数据都连在一起,再读取的话就gg;

              修复的方法也很简单

              //在class type里的插入流重载中--------------------
              friend ostream & operator<<(ostream & out, blu &cu) {
              	out << cu.s << '\t' << cu.c << '\t' << cu.p;
              	return out;
              }
              //-----------------------------------------------
              
              void readFile() {
              	char str[100];
              	ifstream fkb("fkb.txt");
              	while (fkb.getline(str, 100)) {
              		cout << str << endl;
              	}
              }
              
              void writeFile() {
              	type type0;
              	ofstream fkb("fkb.txt", ios::app);
              	cin >> type0;
              	fkb << type0 << endl;
              	fkb.close();
              }
              
              1 条回复 最后回复
              0
              • RNAtlance203R RNAtlance203

                还有个问题,就是如果私有或者受保护成员是字符串或者数组的话怎么整
                (史前大坑)

                RNAtlance203R 离线
                RNAtlance203R 离线
                RNAtlance203
                写于 最后由 编辑
                #7

                @rnatlance203 在 C++中流重载使用方法 中说:

                还有个问题,就是如果私有或者受保护成员是字符串或者数组的话怎么整
                (史前大坑)

                我发现我就是个人才,就算挖坑都不会把自己坑到

                class blu {
                protected:
                	char s[100];
                	char c[100];
                	char p;
                public:
                	blu() {
                		strcpy(s, "fsk");
                		strcpy(c, "fkb");
                		p = 'z';
                	}
                	~blu() {
                
                	}
                	friend istream & operator>>(istream & in, blu &cu) {
                		in >> cu.s >> cu.c >> cu.p;
                		return in;
                	}
                	friend ostream & operator<<(ostream & out, blu &cu) {
                		out << cu.s << '\t' << cu.c << '\t' << cu.p;
                		return out;
                	}
                };
                

                这不是跟上面的整型变量一样了吗 成员函数都不用写了,爽死

                1 条回复 最后回复
                0
                回复
                • 在新帖中回复
                登录后回复
                • 从旧到新
                • 从新到旧
                • 最多赞同


                • 登录

                • 没有帐号? 注册

                • 登录或注册以进行搜索。
                • 第一个帖子
                  最后一个帖子
                0
                • 版块
                • 最新
                • 标签
                • 热门
                • 用户
                • 群组
                • 搜索