-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from 2024zht/C++-Data-Types-and-Literals
feat(CPP): Add C++ Data Types and Literals
- Loading branch information
Showing
14 changed files
with
2,280 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# C++ 中的类型转换 | ||
|
||
类型转换基本上是从一种类型转换为另一种类型。C++ 中有两种类型转换: | ||
1.**隐式类型转换** 也称为“自动类型转换”。 | ||
|
||
- 由编译器自行完成,无需用户的外部触发。 | ||
- 通常在表达式中有多个数据类型存在时发生。在这种情况下,为了避免数据丢失,会发生类型转换(类型提升)。 | ||
- 所有变量的数据类型都会升级为具有最大数据类型的变量的数据类型。 | ||
|
||
``` | ||
bool -> char -> short int -> int -> | ||
unsigned int -> long -> unsigned -> | ||
long long -> float -> double -> long double | ||
``` | ||
|
||
隐式转换可能会丢失信息,可能会丢失符号(当有符号类型隐式转换为无符号类型时),并且可能会发生溢出(当 `long long` 隐式转换为 `float` 时)。 | ||
|
||
**隐式类型转换的示例:** | ||
|
||
``` | ||
// 以下是一个 C++ 中隐式类型转换的例子: | ||
#include <iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
int x = 10; // 整型数x | ||
char y = 'a'; // 字符型y | ||
// 字符 'a' 隐式转换为 int 时,其 ASCII 值为 97。 | ||
x = x + y; | ||
// 变量 x 隐式转换为 float。 | ||
float z = x + 1.0; | ||
cout << "x = " << x << endl | ||
<< "y = " << y << endl | ||
<< "z = " << z << endl; | ||
return 0; | ||
} | ||
``` | ||
|
||
**输出** | ||
|
||
``` | ||
x = 107 | ||
y = a | ||
z = 108 | ||
``` | ||
|
||
2.**显式类型转换:**这个过程也称为类型转换,是用户定义的。用户可以在这里将结果类型转换为特定的数据类型。 | ||
|
||
在 C++ 中,可以通过两种方式进行显式类型转换: | ||
|
||
- **通过赋值转换**:这是通过在表达式前用括号明确指定所需类型来完成的。这也可以被认为是强制转换。 | ||
|
||
**语法** | ||
|
||
``` | ||
(type) expression | ||
``` | ||
|
||
其中 type 表示最终结果将被转换成的数据类型。 | ||
|
||
**样例** | ||
|
||
``` | ||
// C++程序演示显式类型转换 | ||
#include <iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
double x = 1.2; | ||
// 从 double 到 int 的显式转换 | ||
int sum = (int)x + 1; | ||
cout << "Sum = " << sum; | ||
return 0; | ||
} | ||
``` | ||
|
||
**输出** | ||
|
||
``` | ||
Sum = 2 | ||
``` | ||
|
||
- **使用类型转换运算符进行转换:**类型转换运算符是一种**一元运算符**,它强制将一种数据类型转换为另一种数据类型。 | ||
|
||
C++ 支持四种类型转换: | ||
|
||
1. **静态转换(Static Cast)** | ||
2. **动态转换(Dynamic Cast)** | ||
3. **常量转换(Const Cast)** | ||
4. **重解释转换(Reinterpret Cast)** | ||
|
||
**样例** | ||
|
||
``` | ||
#include <iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
float f = 3.5; | ||
// 使用类型转换运算符 | ||
int b = static_cast<int>(f); | ||
cout << b; | ||
} | ||
``` | ||
|
||
### 类型转换的优势: | ||
1.这可以利用类型层次结构或类型表示的某些特性。 | ||
2.它有助于计算包含不同数据类型变量的表达式。 | ||
|
||
|
||
|
161 changes: 161 additions & 0 deletions
161
Data Types and Literals/cpp-Data-Type-Ranges-and-Their-Macros.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# C++ 中的数据类型范围及其宏 | ||
|
||
在竞争性编程中,通常需要将变量赋值为数据类型所能容纳的最大值或最小值。然而,记住这些大而精确的数字是一项困难的任务。因此,C++ 提供了一些宏来表示这些数字,这样可以直接将它们赋值给变量,而不必实际输入整个数字。以下是一些宏的列表: | ||
|
||
<table><thead><tr><th style="width: 116.667px;"><p dir="ltr"><span>数据类型</span></p> | ||
</th><th style="width: 116.667px;"><p dir="ltr"><span>范围 </span></p> | ||
</th><th style="width: 116.667px;"><p dir="ltr"><span>宏的最小值 </span></p> | ||
</th><th style="width: 116.667px;"><p dir="ltr"><span>宏的最大值 </span></p> | ||
</th></tr></thead><tbody><tr><th style="width: 140px;"><p dir="ltr"><span> char </span></p> | ||
</th><td style="width: 140px;"><p dir="ltr"><span>-128 to +127 </span></p><div id="GFG_AD_Desktop_InContent_ATF_336x280" style="text-align:center; max-height: 280px;"></div><div id="GFG_AD_gfg_mobile_336x280_1" style="margin: 5px 0;"></div> | ||
</td><td style="width: 140px;"><p dir="ltr"><span>CHAR_MIN </span></p> | ||
</td><td style="width: 140px;"><p dir="ltr"><span>CHAR_MAX </span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>short char </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>-128 to +127 </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>SCHAR_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>SCHAR_MAX</span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>unsigned char </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>0 to 255 </span></p> | ||
</td><td style="width: 116.667px;"> | ||
<p><span>—</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>UCHAR_MAX </span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>short int </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>-32768 to +32767 </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>SHRT_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>SHRT_MAX </span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>unsigned short int </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>0 to 65535 </span></p> | ||
</td><td style="width: 116.667px;"> | ||
<p><span>—</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>USHRT_MAX</span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>int </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>-2147483648 to +2147483647 </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>INT_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>INT_MAX </span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>unsigned int </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>0 to 4294967295 </span></p> | ||
</td><td style="width: 116.667px;"> | ||
<p><span>—</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>UINT_MAX </span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>long int </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>-9223372036854775808 to +9223372036854775807</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>LONG_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>LONG_MAX</span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>unsigned long int</span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>0 to 18446744073709551615</span></p> | ||
</td><td style="width: 116.667px;"> | ||
<p><span>—</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>ULONG_MAX</span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>long long int</span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>-9223372036854775808 to +9223372036854775807</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>LLONG_MIN</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>LLONG_MAX</span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>unsigned long long int</span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>0 to 18446744073709551615 </span></p> | ||
</td><td style="width: 116.667px;"> | ||
<p><span>—</span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>ULLONG_MAX</span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>float </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>1.17549e-38 to 3.40282e+38 </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>FLT_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>FLT_MAX </span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>float (negative)</span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>-1.17549e-38 to -3.40282e+38 </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>-FLT_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>-FLT_MAX</span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>double</span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>2.22507e-308 to 1.79769e+308 </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>DBL_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>DBL_MAX </span></p> | ||
</td></tr><tr><th style="width: 116.667px;"><p dir="ltr"><span>double (negative) </span></p> | ||
</th><td style="width: 116.667px;"><p dir="ltr"><span>-2.22507e-308 to -1.79769e+308 </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>-DBL_MIN </span></p> | ||
</td><td style="width: 116.667px;"><p dir="ltr"><span>-DBL_MAX </span></p> | ||
</td></tr></tbody></table> | ||
|
||
**样例** | ||
|
||
以下示例展示了数据类型的宏。 | ||
|
||
``` | ||
// 演示数据类型宏的 C++ 代码 | ||
#include <float.h> // 对于浮点数,双精度浮点数的宏 | ||
#include <iostream> | ||
#include <limits.h> // 对于整型,字符型宏 | ||
using namespace std; | ||
int main() | ||
{ | ||
//使用宏显示数据类型范围 | ||
cout << "char ranges from: " << CHAR_MIN << " to " | ||
<< CHAR_MAX << endl; | ||
cout << "\nshort char ranges from: " << SCHAR_MIN | ||
<< " to " << SCHAR_MAX << endl; | ||
cout << "\nunsigned char ranges from: " << 0 << " to " | ||
<< UCHAR_MAX << endl; | ||
cout << "\n\nshort int ranges from: " << SHRT_MIN | ||
<< " to " << SHRT_MAX << endl; | ||
cout << "\nunsigned short int ranges from: " << 0 | ||
<< " to " << USHRT_MAX << endl; | ||
cout << "\nint ranges from: " << INT_MIN << " to " | ||
<< INT_MAX << endl; | ||
cout << "\nunsigned int ranges from: " << 0 << " to " | ||
<< UINT_MAX << endl; | ||
cout << "\nlong int ranges from: " << LONG_MIN << " to " | ||
<< LONG_MAX << endl; | ||
cout << "\nunsigned long int ranges from: " << 0 | ||
<< " to " << ULONG_MAX << endl; | ||
cout << "\nlong long int ranges from: " << LLONG_MIN | ||
<< " to " << LLONG_MAX << endl; | ||
cout << "\nunsigned long long int ranges from: " << 0 | ||
<< " to " << ULLONG_MAX << endl; | ||
cout << "\n\nfloat ranges from: " << FLT_MIN << " to " | ||
<< FLT_MAX << endl; | ||
cout << "\nnegative float ranges from: " << -FLT_MIN | ||
<< " to " << -FLT_MAX << endl; | ||
cout << "\ndouble ranges from: " << DBL_MIN << " to " | ||
<< DBL_MAX << endl; | ||
cout << "\nnegative double ranges from: " << -DBL_MIN | ||
<< " to " << -DBL_MAX << endl; | ||
return 0; | ||
} | ||
``` | ||
|
||
**输出** | ||
|
||
``` | ||
char ranges from: -128 to 127 | ||
short char ranges from: -128 to 127 | ||
unsigned char ranges from: 0 to 255 | ||
short int ranges from: -32768 to 32767 | ||
unsigned short int ranges from: 0 to 65535 | ||
int ranges from: -2147483648 to 2147483647 | ||
unsigned int ranges from: 0 to 4294967295 | ||
long int ranges from: -9223372036854775808 to 9223372036854775807 | ||
unsigned long int ranges from: 0 to 18446744073709551615 | ||
long long int ranges from: -9223372036854775808 to 9223372036854775807 | ||
unsigned long long int ranges from: 0 to 18446744073709551615 | ||
float ranges from: 1.17549e-38 to 3.40282e+38 | ||
negative float ranges from: -1.17549e-38 to -3.40282e+38 | ||
double ranges from: 2.22507e-308 to 1.79769e+308 | ||
negative double ranges from: -2.22507e-308 to -1.79769e+308 | ||
``` | ||
|
Oops, something went wrong.