1 常变量

1 int 变量

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;

int main() {
//变量创建的语法:数据类型 变量名 = 变量初始值 下方示例int整形
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
#define day 7

int main3() {

cout << "一周里面有:" << day << "天" << endl;
//day = 8; //报错,宏常量不可以修改

//法二:const修饰变量
const int month = 12;

cout << "一年里面有:" << month << "月" << endl;

///month = 12 会报错,=左边必须是可以改变的值

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
// 方式1:逐个赋值
Student stu1;
stu1.id = 1001;
stu1.name = "李四";

// 方式2:初始化列表
Student stu2 = {1002, "王五", 19, 88.5};

// 方式3:C++11统一初始化
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;
}

结构体的优势

  1. 数据组织:把相关的数据组织在一起
  2. 代码清晰:提高代码的可读性和可维护性
  3. 传递方便:可以一次性传递多个相关数据
  4. 扩展性强:可以轻松添加新的成员

与类的区别

在C++中,structclass 很像,主要区别是:

  • 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 表示这个函数不返回任何值
void printHello() {
cout << "Hello, World!" << endl;
// 不需要 return 语句
// 或者可以用 return; (不带返回值)
}

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;

// void 表示这个函数不接受任何参数
int getNumber(void) {
return 42;
}

// 等价写法(C++中通常省略void)
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* 可以指向任何类型的数据
void* ptr;

ptr = &a; // 指向int
cout << "int值: " << *(int*)ptr << endl;

ptr = &b; // 指向double
cout << "double值: " << *(double*)ptr << endl;

ptr = &c; // 指向char
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

重要特点:

  1. void函数不需要return语句,或者可以用 return;(不带值)
  2. void不能定义变量void x; 是错误的
  3. void*需要类型转换:使用前需要转换为具体类型

简单来说,**void最主要的意思就是”无”或”空”**,用来表示没有返回值或没有参数。


cinscanfprintf的区别与用法


点击展开知识点

一、基本概念与用法

  • 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;
      }
    • 输入规则:通过格式控制字符串指定要读取的数据类型和格式,对于基本数据类型(如 intfloat 等),需要在变量名前加上取地址符 & ,将变量的地址传递给函数,以便将读取的数据存储到对应变量中。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 :在默认情况下,cinstdin 是同步的(通过 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 :虽然可以灵活控制输出格式,但需要熟悉各种格式说明符,在处理复杂数据类型或者需要动态生成输出格式时,编写代码的难度会增加。