个人学习笔记
2025-07-30 21:50:47
发布于:江苏
26阅读
0回复
0点赞
结构体
#include <bits/stdc++.h>
using namespace std;
struct stu{ //stu 自定义类型
int id;
string name;
int age;
double score;
};
stu a;
int main(){
a.id = 1;
a.name = "张飞";
a.age = 28;
a.score = 59;
stu b = {2, "诸葛亮", 31, 100};
stu c = {3, "李华", 10, 95};
stu d = {4, "刘备", 40, 99};
cout << a.id << ' ';
cout << d.name << endl;
return 0;
}
sort
#include <bits/stdc++.h>
using namespace std;
//自定义的比较规则
bool cmp(int x, int y){
return x > y;
}
int main(){
int a[110] = {0,1,34,23,5,98,8,9,21, 90, 10};
int n = 10;
// for (int i=1; i<=n; i++) cin >> a[i];
// for (int i=0; i<10; i++) cout<<a[i]<<' ';
for (int i=1; i<=10; i++) cout<<a[i]<<' ';
cout<<endl;
// sort(a, a+n);
sort(a+1, a+n+1, cmp);
/*
数组名称表示首个数组元素的地址
第1个参数:开始排序的地址
第2个参数:结束排序的地址
第3个参数:(可以省略) 自定义的比较规则
*/
for (int i=1; i<=10; i++) cout<<a[i]<<' ';
// cout<<endl;
// for (int i=10; i>=1; i--) cout<<a[i]<<' ';
return 0;
}
内存单位
#include <bits/stdc++.h>
using namespace std;
//全局变量 静态存储区 2G
int main(){
//局部变量 栈空间 2M
// int a[5000][5000] = {};
cout << sizeof (int) << endl; // 4 Byte
cout << sizeof (long long) << endl; // 8 Byte
cout << sizeof (short) << endl; // 2 Byte
cout << sizeof (char) << endl; // 1 Byte
cout << sizeof (bool) << endl; // 1 Byte
cout << sizeof (double) << endl; // 8 Byte
cout << sizeof (float) << endl; // 4 Byte
return 0;
}
#if 0
bit (比特位)位,bit 最小单位
Byte (字节) 常用单位
KB (千字节)
MB (兆字节)
GB (吉字节)
TB
...
1TB = 1024GB
1GB = 1024MB
1MB = 1024KB
1KB = 1024B
1B = 8bit (比特位,二进制位)
#endif
小数的位数控制(两种方式)
质数&函数
#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
//1. 函数的声明
int prime(int x);
//2. 函数function:(功能)的定义
int prime(int x){ //形参 :用来接受的
for (int i=2; i*i<=x; i++){
// for (int i=2; i<=sqrt(x); i++){
// for (int i=2; i<=x-1; i++){
if (x%i == 0) return 0;
}
return 1;
}
int main(){
int cnt = 0;
for (int i=2; i<=1000; i++){
if (prime(i)){ //3. 函数的调用 (实参)
cnt++;
cout<<i<<' ';
}
}
cout<<endl << cnt;
return 0;
}
这里空空如也
有帮助,赞一个