文件操作
// 我们只需要学会两个语句即可
// 输入输出文件,和.cpp放在同一个文件夹里即可
// 实际操作中,复赛会要求文件输入输出
// 注意输入输出文件的命名规范
// 测试的时候,一般还会用标准输入输出测试
// 最后还是要用文件输入输入测一下,然后把输入输出文件的名称写对
// 最后的最后,检查文件夹命名、.cpp命名、输入输出文件命名
#include <stdio.h>
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
结构体 struct
在实际问题中,一组数据往往有不同的数据类型,比如学生,有姓名、学号、语文成绩、数学成绩等等。C++语言给出了一种构造数据类型—“结构体”,结构体在数据存储方面相当于其他高级语言中的记录,但它悠着面向对象的优势。
struct stu{
string name;
int id;
int chinese, math;
};
结构体的构造函数
https://blog.csdn.net/a_forever_dream/article/details/88867801
// struct构造函数,说白了,就是初始化
struct stu{
string name;
int id;
int chinese, math;
stu(){}
stu(string a, int b, int c, int d){
name = a, id = b, chinese = c, math = d;
}
};
// 构造函数,具体展开讲解
struct node(){
int x, y;
node(){}
node(int c){
x = 0, y = c; // 这种写法严格是赋值,不是初始化
}
node(int c):x(0), y(c){} // 这种写法是初始化, 象征性的大括号不能丢
};
// 举例,看看输出什么
#include <bits/stdc++.h>
using namespace std;
struct node{
int x;
node():x(1){}
};
int main(){
node a;
cout << a.x << '\n';
return 0;
}
// 输出 1
// 一个结构体,可以有多个构造函数,要求每个构造函数的形参表不能一样
#include <bits/stdc++.h>
using namespace std;
struct node{
int x;
node():x(1){}
node(int a):x(a){}
};
int main(){
node a, b(5);
cout << a.x << ' '<< b.x << '\n';
return 0;
}
// 输出 1 5
// 给一个比较具有实战性质的示范代码
// 下面的代码中,node a,这样初始化,当然是可以的
// 但在node b(3, 4)的写法,非常简洁易读,在入队、入堆的操作中,非常省事
#include <bits/stdc++.h>
using namespace std;
struct node{
int x, y;
node(){}
node(int a, int b): x(a), y(b){}
};
int main(){
node a;
a.x = 5, a.y = 6;
node b(3, 4);
printf("%d %d\n", a.x, a.y);
printf("%d %d\n", b.x, b.y);
return 0;
}
文本文件类型与二进制文件类型
二进制文件是把内存中的数据按其在内存中的存储形式原样输出到磁盘上存放,也就是说存放的是数据的原形式。
文本文件是把数据的终端形式的二进制数据输出到磁盘上存放,也就是说存放的是数据的终端形式。