2. 基础数据类型和控制流

基础数据类型

1. 整数类型

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 有符号整数
int8_t i8 = -128;            // -128 到 127
int16_t i16 = 32767;         // -32768 到 32767
int32_t i32 = 2147483647;    // -2147483648 到 2147483647
int64_t i64 = 9223372036854775807LL;  // 64位整数

// 无符号整数
uint8_t u8 = 255;            // 0 到 255
uint16_t u16 = 65535;        // 0 到 65535
uint32_t u32 = 4294967295U;  // 0 到 4294967295
uint64_t u64 = 18446744073709551615ULL;  // 64位无符号整数

// 常用整数类型
short s = 32767;             // 通常16位
int i = 2147483647;          // 通常32位
long l = 2147483647L;        // 32位或64位
long long ll = 9223372036854775807LL;  // 至少64位

2. 浮点类型

1
2
3
4
5
6
7
float f = 3.14159f;          // 单精度浮点数,约7位有效数字
double d = 3.14159265359;    // 双精度浮点数,约15-17位有效数字
long double ld = 3.14159265358979323846L;  // 扩展精度

// 科学计数法
double sci = 1.23e-4;        // 0.000123
double sci2 = 1.23e4;        // 12300.0

3. 字符和字符串

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 字符
char c = 'A';                // 单个字符
wchar_t wc = L'你';          // 宽字符
char16_t c16 = u'你';        // UTF-16字符
char32_t c32 = U'你';        // UTF-32字符

// 字符串
const char* str = "Hello";              // C风格字符串
std::string cpp_str = "Hello";          // C++风格字符串
std::wstring wstr = L"你好";            // 宽字符串
std::u16string str16 = u"你好";         // UTF-16字符串
std::u32string str32 = U"你好";         // UTF-32字符串

4. 布尔类型

1
2
bool flag = true;            // true 或 false
bool result = (1 < 2);       // 比较表达式

5. 空类型

1
2
3
4
5
void function() {            // 无返回值的函数
    // ...
}

int* ptr = nullptr;          // 空指针(C++11)

变量声明和初始化

1. 变量声明

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 基本声明
int count;                   // 默认初始化
int value = 42;             // 复制初始化
int number(42);             // 直接初始化
int data{42};               // 统一初始化(C++11)

// const 常量
const int MAX_VALUE = 100;   // 常量声明
constexpr int SIZE = 42;     // 编译期常量(C++11)

// auto 类型推导
auto x = 42;                 // int
auto y = 3.14;              // double
auto str = "Hello";          // const char*

2. 类型转换

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// 隐式转换
int i = 42;
double d = i;               // int 转 double

// 显式转换
double pi = 3.14159;
int rounded = static_cast<int>(pi);  // double 转 int

// C风格转换(不推荐)
int old_style = (int)pi;

运算符

1. 算术运算符

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int a = 10, b = 3;
int sum = a + b;            // 加法: 13
int diff = a - b;           // 减法: 7
int prod = a * b;           // 乘法: 30
int quot = a / b;           // 整除: 3
int rem = a % b;            // 取余: 1

// 自增自减
int x = 5;
++x;                        // 前缀自增: 6
x++;                        // 后缀自增: 7
--x;                        // 前缀自减: 6
x--;                        // 后缀自减: 5

2. 比较运算符

1
2
3
4
5
6
bool equal = (a == b);      // 相等
bool not_equal = (a != b);  // 不相等
bool greater = (a > b);     // 大于
bool less = (a < b);        // 小于
bool ge = (a >= b);         // 大于等于
bool le = (a <= b);         // 小于等于

3. 逻辑运算符

1
2
3
4
5
6
7
bool p = true, q = false;
bool conjunction = p && q;  // 逻辑与
bool disjunction = p || q;  // 逻辑或
bool negation = !p;         // 逻辑非

// 短路求值
bool result = (p && someFunction());  // 如果p为false,不会调用someFunction

控制流语句

1. 条件语句

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// if-else
if (x > 0) {
    std::cout << "Positive";
} else if (x < 0) {
    std::cout << "Negative";
} else {
    std::cout << "Zero";
}

// switch
switch (grade) {
    case 'A':
        std::cout << "Excellent";
        break;
    case 'B':
        std::cout << "Good";
        break;
    default:
        std::cout << "Unknown";
}

// 条件运算符
int abs_value = (x >= 0) ? x : -x;

2. 循环语句

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// for循环
for (int i = 0; i < 10; ++i) {
    std::cout << i << " ";
}

// 范围for循环(C++11)
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (const auto& num : numbers) {
    std::cout << num << " ";
}

// while循环
int count = 0;
while (count < 5) {
    std::cout << count++ << " ";
}

// do-while循环
int num;
do {
    std::cout << "Enter a number (0 to quit): ";
    std::cin >> num;
} while (num != 0);

3. 跳转语句

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// break
for (int i = 0; i < 10; ++i) {
    if (i == 5) break;      // 跳出循环
    std::cout << i << " ";
}

// continue
for (int i = 0; i < 10; ++i) {
    if (i % 2 == 0) continue;  // 跳过偶数
    std::cout << i << " ";
}

// return
int sum(int a, int b) {
    return a + b;           // 返回并结束函数
}

// goto(不推荐使用)
int i = 0;
start:
    if (i < 5) {
        std::cout << i++ << " ";
        goto start;
    }

输入输出

1. 标准输入输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// 输出
std::cout << "Hello" << std::endl;  // 输出字符串并换行
std::cout << "Value: " << 42 << "\n";  // 输出多个值

// 输入
int number;
std::cout << "Enter a number: ";
std::cin >> number;

// 字符串输入
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);  // 读取整行

2. 格式化输出

1
2
3
4
5
6
7
#include <iomanip>

double pi = 3.14159265359;
std::cout << std::fixed << std::setprecision(2) << pi << std::endl;  // 3.14

// 设置字段宽度
std::cout << std::setw(10) << std::right << "Hello" << std::endl;  // "     Hello"

57.12k 字
43篇文章