1 常变量
1 int 变量
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include<iostream> using namespace std;
int main() { int a = 10;
cout <<"a = "<< a << endl;
system("pause");
return 0; }
|
2 常量
2.1 #define宏常量
2.2 const修饰的变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include<iostream> using namespace std;
#define day 7
int main3() {
cout << "一周里面有:" << day << "天" << endl;
const int month = 12;
cout << "一年里面有:" << month << "月" << endl;
system("pause");
return 0;
}
|
知识点
结构体(struct) 是C/C++中一种自定义的数据类型,它允许你将多个不同类型的数据组合在一起。
点击展开知识点
基本概念
结构体就像是一个”容器”,可以把相关的数据打包在一起。
基本语法
1 2 3 4 5 6
| struct 结构体名 { 数据类型 成员1; 数据类型 成员2; };
|
简单例子
1 2 3 4 5 6 7
| struct Student { int id; string name; int age; double score; };
|
使用结构体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include <iostream> #include <string> using namespace std;
struct Student { int id; string name; int age; double score; };
int main() { Student stu1; stu1.id = 1001; stu1.name = "张三"; stu1.age = 18; stu1.score = 95.5; cout << "学号: " << stu1.id << endl; cout << "姓名: " << stu1.name << endl; cout << "年龄: " << stu1.age << endl; cout << "成绩: " << stu1.score << endl; return 0; }
|
初始化结构体
1 2 3 4 5 6 7 8 9 10
| Student stu1; stu1.id = 1001; stu1.name = "李四";
Student stu2 = {1002, "王五", 19, 88.5};
Student stu3{1003, "赵六", 20, 92.0};
|
结构体数组
1 2 3 4 5 6 7 8
| Student class1[3] = { {1001, "张三", 18, 95.5}, {1002, "李四", 19, 88.0}, {1003, "王五", 20, 76.5} };
cout << class1[0].name << "的成绩是: " << class1[0].score << endl;
|
结构体作为函数参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| void printStudent(Student s) { cout << "学号: " << s.id << endl; cout << "姓名: " << s.name << endl; cout << "年龄: " << s.age << endl; cout << "成绩: " << s.score << endl; }
void setScore(Student &s, double newScore) { s.score = newScore; }
int main() { Student stu{1001, "张三", 18, 95.5}; printStudent(stu); setScore(stu, 98.0); printStudent(stu); return 0; }
|
实际应用例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <iostream> #include <string> using namespace std;
struct Point { int x; int y; };
struct Rectangle { Point topLeft; Point bottomRight; };
int main() { Point p1 = {0, 0}; Point p2 = {10, 5}; Rectangle rect = {p1, p2}; int width = rect.bottomRight.x - rect.topLeft.x; int height = rect.bottomRight.y - rect.topLeft.y; int area = width * height; cout << "矩形面积: " << area << endl; return 0; }
|
结构体的优势
- 数据组织:把相关的数据组织在一起
- 代码清晰:提高代码的可读性和可维护性
- 传递方便:可以一次性传递多个相关数据
- 扩展性强:可以轻松添加新的成员
与类的区别
在C++中,struct 和 class 很像,主要区别是:
struct 默认成员是 public(公开的)
class 默认成员是 private(私有的)
结构体是C++中非常重要的概念,特别适合用来表示现实世界中的实体,如学生、员工、坐标点等。
void 在C++中有几种不同的含义,主要取决于使用的上下文:
点击展开知识点
1. 函数返回类型 - 无返回值
当 void 用作函数返回类型时,表示该函数不返回任何值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <iostream> using namespace std;
void printHello() { cout << "Hello, World!" << endl; }
void greet(string name) { cout << "Hello, " << name << "!" << endl; }
int main() { printHello(); greet("Alice"); return 0; }
|
2. 函数参数 - 无参数
当 void 用作函数参数时,表示该函数不接受任何参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <iostream> using namespace std;
int getNumber(void) { return 42; }
int getNumber() { return 42; }
int main() { int num = getNumber(); cout << "数字是: " << num << endl; return 0; }
|
3. 空指针 - void*
void* 表示通用指针,可以指向任何数据类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream> using namespace std;
int main() { int a = 10; double b = 3.14; char c = 'X'; void* ptr; ptr = &a; cout << "int值: " << *(int*)ptr << endl; ptr = &b; cout << "double值: " << *(double*)ptr << endl; ptr = &c; cout << "char值: " << *(char*)ptr << endl; return 0; }
|
4. 实际应用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <iostream> using namespace std;
void showMenu() { cout << "=== 菜单 ===" << endl; cout << "1. 开始游戏" << endl; cout << "2. 设置" << endl; cout << "3. 退出" << endl; cout << "============" << endl; }
void calculateSum(int a, int b) { int sum = a + b; cout << a << " + " << b << " = " << sum << endl; }
int getRandomNumber() { return rand() % 100; }
int main() { showMenu(); calculateSum(5, 3); int num = getRandomNumber(); cout << "随机数: " << num << endl; return 0; }
|
总结:
| 使用场景 |
含义 |
示例 |
| 函数返回类型 |
函数不返回值 |
void func() |
| 函数参数 |
函数不接受参数 |
int func(void) |
| 指针类型 |
通用指针 |
void* ptr |
重要特点:
- void函数不需要return语句,或者可以用
return;(不带值)
- void不能定义变量:
void x; 是错误的
- void*需要类型转换:使用前需要转换为具体类型
简单来说,**void最主要的意思就是”无”或”空”**,用来表示没有返回值或没有参数。
cin、scanf、printf的区别与用法
点击展开知识点
一、基本概念与用法
cin
- 所属头文件:
<iostream> ,是 C++ 标准输入流对象,用于从标准输入设备(通常是键盘)读取数据。
- 用法示例:
1 2 3 4 5 6
| #include <iostream> int main() { int num; std::cin >> num; return 0; }
|
- 输入规则:使用提取运算符
>>,它会自动跳过输入中的空白字符(空格、制表符、换行符等),直到遇到有效数据才开始读取。当读取不同类型数据时,会自动进行类型转换,但如果输入数据类型与目标变量类型不匹配,会导致输入失败,后续输入操作可能出现问题。
scanf
- 所属头文件:
<stdio.h> ,是 C 语言中用于格式化输入的函数。
- 用法示例:
1 2 3 4 5 6
| #include <stdio.h> int main() { int num; scanf("%d", &num); return 0; }
|
- 输入规则:通过格式控制字符串指定要读取的数据类型和格式,对于基本数据类型(如
int、float 等),需要在变量名前加上取地址符 & ,将变量的地址传递给函数,以便将读取的数据存储到对应变量中。scanf 也会跳过空白字符,但如果输入数据与格式控制字符串不匹配,会导致读取错误,并且会使输入缓冲区残留错误数据,影响后续输入操作。
printf
- 所属头文件:
<stdio.h> ,是 C 语言中用于格式化输出的函数。
- 用法示例:
1 2 3 4 5 6
| #include <stdio.h> int main() { int num = 10; printf("数字是 %d\n", num); return 0; }
|
- 输出规则:通过格式控制字符串指定输出数据的类型和格式,后面跟随要输出的变量列表。格式控制字符串中可以包含普通字符、转义字符和格式说明符(如
%d 表示输出整数,%f 表示输出浮点数等),可以灵活控制输出的格式和内容。
二、区别
类型安全性
cin :在 C++ 的类型安全机制下工作,输入时会根据变量类型进行类型检查和转换,如果输入数据类型不匹配,会设置错误标志,阻止进一步输入操作,从而避免一些潜在的错误。
scanf :C 语言函数,类型安全性相对较弱。如果格式控制字符串与实际传入的参数类型不匹配,可能导致程序崩溃或者产生未定义行为,例如使用 %d 读取一个 float 类型数据时。
printf :同样是 C 语言函数,类型安全性较弱。如果格式控制字符串与实际输出的参数类型不匹配,可能导致输出结果错误,甚至程序崩溃。
效率
cin :在默认情况下,cin 与 stdin 是同步的(通过 std::ios::sync_with_stdio(true) 控制,默认开启),这会导致它的效率相对较低。当关闭同步(std::ios::sync_with_stdio(false) ,并且不和 scanf 等 C 输入函数混用)时,效率会有所提升,但可能与 C 标准输入输出函数不兼容。
scanf :C 语言的底层输入函数,直接操作标准输入流,在处理大量简单数据输入时,通常比 cin 效率更高。
printf :C 语言的底层输出函数,直接操作标准输出流,在格式化输出大量数据时,效率较高。
易用性
cin :使用提取运算符>>,语法简洁直观,对于 C++ 开发者来说比较符合面向对象的编程习惯,尤其在处理 C++ 自定义类型的输入时,通过重载>>运算符,可以方便地实现自定义类型的输入。
scanf :需要严格按照格式控制字符串来编写代码,并且要注意变量地址的传递,对于初学者来说,相对容易出错,尤其是在处理复杂数据类型的输入时,代码编写较为繁琐。
printf :虽然可以灵活控制输出格式,但需要熟悉各种格式说明符,在处理复杂数据类型或者需要动态生成输出格式时,编写代码的难度会增加。
Author:
Lyxy-szs
Permalink:
http://lyxy-szs.github.io/2025/09/11/C++¬e1/
Slogan:
永远相信美好的事情即将發生?