32-初探C++标准库

32-初探C++标准库

  • C++标准库并不是C++语言的一部分
  • C++标准库是由类库和函数库组合的集合
  • C++标准库中定义的类和对象都位于std命名空间中
  • C++标准库的头文件都不带.h后缀
  • C++标准库涵盖了C库的功能

C++标准库预定义了多数常用的数据结构:

  • --<bitset>    --<set>    --<cstdio>   
  • --<deque>    --<stack>    --<cstring>
  • --<list>    --<vector>    --<cstdlib>
  • --<queue>    --<map>    --<cmath>
【范例代码】C++标准库的C库兼容
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(int argc, const char *argv[]) {
    printf("Hello world!\n");
    
    char* p = (char*)malloc(16);
    
    strcpy(p, "D.T.Software");
    
    double a = 3;
    double b = 4;
    double c = sqrt(a * a + b * b);
    
    printf("c = %f\n", c);
    
    free(p); 
    return 0;
}

C++标准库中的显示器对象cout,C++标准库中的键盘对象cin。

【范例代码】C++中的输入输出

#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, const char *argv[]) {
    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;
}

猜你喜欢

转载自blog.csdn.net/qq_19247455/article/details/80137624