您的位置:首页 > 健康 > 养生 > c++:C++标准库学习iostream

c++:C++标准库学习iostream

2024/10/5 20:23:24 来源:https://blog.csdn.net/qq_43441284/article/details/139747986  浏览:    关键词:c++:C++标准库学习iostream

基础介绍

  • C程序可以通过__cplusplus符号是否预定义来判断当前是gcc还是g++编译
    __cplusplus的值是long int类型的,值表示当前编译程序的C++编译器的版本号。
 cout << "__cpulspuls :" << __cplusplus << endl;
  • C++文件名的常用后缀:源文件(.cpp .cxx .cc .c .c++),头文件(.hpp .hxx .h)

  • C++更建议的头文件包含形式不是<stdio.h>这样,而是这样
    要点:C++的标准库的头文件是没有后缀名的

  • C++标准库介绍
    (1)C标准库即为C++标准库的一部分,完全继承并以C++方式重写,位于std命名空间中
    (2)C++面向对象库,如string、iostream等,位于std命名空间中
    (3)C++ STL标准模板库,如vector、map等,位于std命名空间中

iostream的cout使用

基本使用
(1)cout即标准输出,对应stdout
(2)cout定义在std命名空间中,要按三种使用方法来用
(3)结合<<符号(流操作符)进行输出,可多节连接
(4)cout涉及的头文件有 <bits/ios_base.h>
(5)cout本质上是ostream(iostream的派生类)的一个对象
(6)流操作符<<本质上是左移运算符在iostream中的运算符重载

  int val = 19;cout << "__cpulspuls :" << __cplusplus << endl;cout << "__cpulspuls :0x" << hex << __cplusplus << "--" << val << "--" << dec<< val << endl;double dl = 1.23456789;cout << "dl :" << dl << endl;cout << "dl :" << setprecision(3) << dl << endl;
  int i = 110;cout << i << endl;cout << dec << i << endl;cout << oct << i << endl;cout << hex << i << endl;cout << setiosflags(ios::uppercase);//使输出的字母(如十六进制中的 A-F)大写cout << hex << i << endl;cout << setbase(8) << i << endl;//表示接下来输出的数字将以八进制格式输出
	double i = 1314.1415926;cout << i << endl;cout << setprecision(3) << i << endl;//设置输出的精度为3位有效数字cout << setprecision(9) << i << endl;//设置输出的精度为9位有效数字cout << setiosflags(ios::fixed);//设置流的格式为固定小数点表示法cout << i << endl;cout << fixed << setprecision(3) << i << endl;//设置小数点后的位数为3cout << setprecision(9) << fixed <<  i << endl;//设置输出的精度为9位有效数字
  double i = 1314.1415926;double j = 42;double k = -42.0;// 原始输出cout << i << endl;cout << j << endl;cout << k << endl;cout << "使用 setprecision(3) 和 showpoint" << endl;//setprecision(3) 在 defaultfloat 模式下显示3位有效数字,showpoint 强制显示小数点cout << setprecision(3) << showpoint;cout << i << endl;cout << j << endl;cout << k << endl;cout << "使用 setprecision(9), fixed 和 showpoint" << endl;//设置为固定小数点模式并显示9位小数,showpoint 强制显示小数点cout << setprecision(9) << fixed << showpoint;cout << i << endl;cout << j << endl;cout << k << endl;cout << "使用 showpos, showpoint 和 fixed" << endl;//showpos 强制显示正号fixed 和 showpoint 使得数字以固定小数点模式显示,setprecision(3) 显示3位小数cout << showpos << fixed << showpoint;cout << setprecision(3);cout << i << endl;cout << j << endl;cout << k << endl;
  int a = 42;double b = 3.14159;string str = "Hello";// 默认显示cout << "Default display:" << endl;cout << a << endl;cout << b << endl;cout << str << endl;// 设置宽度为10, 右对齐 (默认)cout << "\nRight aligned with width 10:" << endl;cout << setw(10) << a << endl;cout << setw(10) << b << endl;cout << setw(10) << str << endl;// 设置宽度为10, 左对齐cout << "\nLeft aligned with width 10:" << endl;cout << left << setw(10) << a << endl;cout << left << setw(10) << b << endl;cout << left << setw(10) << str << endl;// 设置宽度为10, 内部对齐cout << "\nInternal aligned with width 10:" << endl;cout << internal << setw(10) << a << endl;cout << internal << setw(10) << b << endl;cout << internal << setw(10) << str << endl;// 设置宽度为15, 内部对齐cout << "\nInternal aligned with width 15:" << endl;cout << internal << setw(15) << a << endl;cout << internal << setw(15) << b << endl;cout << internal << setw(15) << str << endl;// 设置宽度为10, 填充字符为 '*'cout << "\nRight aligned with width 10 and fill '*':" << endl;cout << setfill('*') << right << setw(10) << a << endl;cout << setfill('-') << right << setw(10) << b << endl;cout << setfill('#') << right << setw(10) << str << endl;

iostream的cin使用

输入数字

  int a, b;cout << "Enter two numbers: ";cin >> a >> b;cout << "The numbers you entered are " << a << " and " << b << endl;cout << "a + b =  " << a + b << endl;

十六进制输入数字

  int a, b;cout << "Enter two numbers: ";cin >> hex >> a >> b;cout << "The numbers you entered are " << a << " and " << b << endl;cout << "a + b =  " << a + b << endl;return 0;

输入字符串

   string name;cout << "Enter your full name: ";getline(cin, name);cout << "Your full name is " << name << endl;return 0;

输入错误检查

   int number;cout << "Enter a number: ";cin >> number;if (cin.fail()) {cout << "Invalid input." << endl;cin.clear(); // 清除错误状态cin.ignore(1000, '\n'); // 忽略错误输入} else {cout << "You entered " << number << endl;}

总结

理解学习C++的三个层次
理解iostream的cout 、cin使用方法

学习记录,侵权联系删除。
来源:朱老师物联网大课堂

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com