基础介绍
- 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;//设置小数点后的位数为3
cout << 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使用方法
学习记录,侵权联系删除。
来源:朱老师物联网大课堂