zoukankan      html  css  js  c++  java
  • 第32课

    第32课 - 初探C++ 标准库

    1. 有趣的重载

      操作符 << 的原生意义是按位左移,例: 1  <<  2 ; 

      其意义是将整数 1 按位左移 2 位,即: 0000 0001   ->    0000 0100

      重载左移操作符,将变量或常量左移到一个对象中!

     1 #include <stdio.h>
     2 
     3 const char endl = '
    ';
     4 
     5 class Console
     6 {
     7 public:
     8     Console& operator << (int i)
     9     {
    10           printf("%d", i);
    11           return *this;
    12     }
    13     
    14         Console& operator << (char c)
    15     {
    16           printf("%c", c);
    17           return *this;
    18     }
    19     
    20     Console& operator << (const char* s)
    21     {
    22           printf("%s", s);
    23           return *this;          
    24     }
    25     
    26         Console& operator << (double d)
    27     {
    28           printf("%f", d);
    29           return *this;          
    30     }
    31 };
    32 
    33 Console cout;
    34 
    35 int main()
    36 {
    37     cout << 1 << endl;               //1
    38     cout << "Hello World!" << endl;  //Hello World!
    39     
    40     double a = 0.1;
    41     double b = 0.2;
    42     
    43     cout << a + b << endl;          //0.300000
    44 
    45     return 0;
    46 }
    重载左移操作符(仿cout类)  //这个例子很微妙,需要仔细揣摩体会

    2. C++ 标准库

      重复发明轮子并不是一件有创造性的事情,站在巨人的肩膀上解决问题会更加有效!

      (1)C++ 标准库并不是C++ 语言的一部分

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

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

      (4)C++ 标准库的头文件都不带 .h 后缀

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

    3. C/C++库

    3.1 C++ 编译环境的组成

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

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

      (3)C++标准库中的C库和 C语言兼容库 在功能上相同,但是两者隶属于不同的模块,使用的头文件不同

      (4)不同厂商提供的C++编译器,C++ 扩展语法模块和编译器扩展模块各不相同

    3.2 C++ 标准库预定义的常用数据结构

      (1)常用的数据结构类:<bitset>、<set>、<deque>、<stack>、<list>、<vector>、<queue>、<map>

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

     1 /*
     2 //C++编译商提供的C兼容库(既不是C++标准库提供的,也不是C语言库文件,而是一种兼容库)
     3 #include <stdio.h>
     4 #include <string.h>
     5 #include <stdlib.h>
     6 #include <math.h>
     7 */
     8 
     9 //C++标准库提供的C兼容库
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <cmath>
    14 
    15 using namespace std; //位于std命名空间中
    16 
    17 int main()
    18 {
    19     //以下的代码是使用C++标准库提供的C兼容库写的,
    20     //从形式上看,与C代码写的完全一样,这就是C++
    21     //为C提供了很好的支持的例子!
    22     printf("hello world!
    ");
    23     
    24     char* p = (char*)malloc(16);
    25     
    26     strcpy(p, "");
    27     
    28     double a = 3;
    29     double b = 4;
    30     double c = sqrt(a * a + b * b);
    31     
    32     printf("c = %f
    ", c);
    33     
    34     free(p);
    35 
    36     return 0;
    37 }
    C++标准库中的C库兼容(如cstdio)

    4. C++输入输出

      

     1 #include <iostream> //使用C++标准库的输入输出
     2 #include <cmath>
     3 
     4 using namespace std; //位于std命名空间中
     5 
     6 int main()
     7 {
     8     cout << "Hello world!" << endl;
     9     
    10     double a = 0;
    11     double b = 0;
    12     
    13     cout <<"Input a:";
    14     cin >> a;
    15     
    16     cout <<"Input b:";
    17     cin >> b;
    18     
    19     double c = sqrt(a * a + b * b);
    20     
    21     cout << "c = "<< c << endl;
    22 
    23     return 0;
    24 }
    C++中的输入输出

    5. 小结

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

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

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

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

  • 相关阅读:
    [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###
    [LeetCode] 22. 括号生成 ☆☆☆(回溯)
    [LeetCode] 15. 3Sum ☆☆☆(3数和为0)
    Trie 树(字典树)
    dubbo框架梳理
    Linux内存管理与C存储空间
    C语言实现的minixml解析库入门教程
    函数不定参数个数的实现
    C语言变量名转字符串的方法
    C语言编译和链接
  • 原文地址:https://www.cnblogs.com/shiwenjie/p/7296430.html
Copyright © 2011-2022 走看看