zoukankan      html  css  js  c++  java
  • 第32课 初探C++标准库(利用操作符重载实现)

    1. 有趣的重载

    (1)操作符<<:原义是按位左移重载“<<”可将变量或常量左移到对象中

    【编程实验】重载左移操作符(仿cout类) 32-1.cpp

    #include <stdio.h>
    
     
    
    const char endl = '
    ';
    
     
    
    class Console
    {
    
    public:
    
        Console& operator << (int i)
    
        {
    
              printf("%d", i);
    
              return *this;
    
        }
    
       
    
        Console& operator << (char c)
    
        {
    
              printf("%c", c);
    
              return *this;
    
        }
    
       
    
        Console& operator << (const char* s)
    
        {
    
              printf("%s", s);
    
              return *this;         
    
        }
    
       
    
        Console& operator << (double d)
    
        {
    
              printf("%f", d);
    
              return *this;         
    
        }
    
    };
    
     
    
    Console cout;
    
     
    
    int main()
    {
    
        cout << 1 << endl;               //1
    
        cout << "Hello World!" << endl;  //Hello World!
    
       
    
        double a = 0.1;
    
        double b = 0.2;
    
       
    
        cout << a + b << endl;          //0.300000
    
     
    
        return 0;
    
    }

    运行结果:

      

    2. C++标准库

    (1)C++标准库

    • C++标准库并不是C++语言的一部分

    • C++标准库是由类库函数库组成的集合

    • C++标准库定义的类和对象都位于std命名空间

    • C++标准库头文件不带.h后缀

    • C++标准库涵盖了C库功能

    (2)C++编译环境的组成

               

    C语言兼容库:头文件带.h,是C++编译器提供商为推广自己的产品,而提供的C兼容库(不是C++标准库提供的)。

    C++标准库:如stringcstdio(注意,不带.h)是C++标准库提供的。使用时要用using namespace std找开命名空间。

    编译器扩展库:编译器自己扩展的

     

    (3)C++标准库预定义的常用数据结构

      ①<bitset>、<set>、<deque>、<stack>、<list>、<vector>、<queue>、<map>

      ②<cstdio>、<cstring>、<cstdlib>、<cmath>——(C++标准库提供的C兼容库!

    【编程实验】C++标准库中的C库兼容(如cstdio) 32-2.cpp

    /*
    
    //C++编译商提供的C兼容库(既不是C++标准库提供的,也不是C语言库文件,而是一种兼容库)
    
    #include <stdio.h>
    
    #include <string.h>
    
    #include <stdlib.h>
    
    #include <math.h>
    
    */
    
     
    
    //C++标准库提供的C兼容库
    
    #include <cstdio>
    
    #include <cstring>
    
    #include <cstdlib>
    
    #include <cmath>
    
     
    
    using namespace std; //位于std命名空间中
    
     
    
    int main()
    {
    
        //以下的代码是使用C++标准库提供的C兼容库写的,
    
        //从形式上看,与C代码写的完全一样,这就是C++
    
        //为C提供了很好的支持的例子!
    
        printf("hello world!
    ");
    
       
    
        char* p = (char*)malloc(16);
    
       
    
        strcpy(p, "");
    
       
    
        double a = 3;
    
        double b = 4;
    
        double c = sqrt(a * a + b * b);
    
       
    
        printf("c = %f
    ", c);
    
       
    
        free(p);
    
     
    
        return 0;
    
    }

    运行结果:

      

    4. C++输入输出

        

    【编程实验】C++中的输入输出  32-3.cpp

    #include <iostream> //使用C++标准库的输入输出
    
    #include <cmath>
    
     
    
    using namespace std; //位于std命名空间中
    
     
    
    int main()
    {
    
        cout << "Hello world!" << endl;
    
       
    
        double a = 0;
    
        double b = 0;
    
       
    
        cout <<"Input a:";
    
        cin >> a;
    
       
    
        cout <<"Input b:";
    
        cin >> b;
    
       
    
        double c = sqrt(a * a + b * b);
    
       
    
        cout << "c = "<< c << endl;
    
     
    
        return 0;
    
    }

    运行结果:

      

    5. 小结

    (1)C++标准库是由类库函数库组成的集合

    (2)C++标准库包含经典算法数据结构实现

    (3)C++标准库涵盖了C库的功能

    (4)C++标准库位于std命名空间中

  • 相关阅读:
    python---装饰器用法小结
    python---mysql事务
    python---sql语句集体更改数据
    python多继承中的深度优先与广度优先
    python---copy
    vue 主次页面区分
    css 过渡动画
    android web外壳
    cordova 打包 守护进程无法启动
    JavaScript 原生控制元素添加删除
  • 原文地址:https://www.cnblogs.com/hoiday/p/10163558.html
Copyright © 2011-2022 走看看