碰见了一串挺有趣的代码,有个铺垫的作用,帮着以后学习和使用
所以下图不是自己写的*10086
顺便把那点能让我自己吹出一大堆彩虹屁的理解记录一下
#include<bits/stdc++.h>//不是自己写的
using namespace std;
int t, num[1001];
string s[1001];//question 1
priority_queue<string, vector<string>, greater<string> > q[1001];//question 2
void work(int hp) {
if(s[t][0] == '*' || hp == 0) return;
if(s[t][0] == 'd') {
for(int i = 1; i <= hp; i++)
printf("| ");
cout << s[t] << endl;
t++; work(hp + 1);
}
if(s[t][0] == 'f') {
q[hp].push(s[t]);
t++; work(hp);
}
if(s[t][0] == ']') {
while(!q[hp].empty()) {
for(int i = 1; i < hp; i++)
printf("| ");
cout << q[hp ].top() << endl;
q[hp].pop();
}
t++; work(hp - 1);
}
}
int main() {
int x = 1, cnt = 1;
num[cnt] = 1;
while(cin >> s[x]) {
if(s[x] == "*") num[++cnt] = x + 1;
++x;
}
x--, cnt--;
for(int i = 1; i <= cnt; i++) {
printf("DATA SET %d:\nROOT\n", i);
t = num[i];
work(1);
while(!q[1].empty()) {
cout << q[1].top() << endl;//question 3
q[1].pop();
}
puts("");
}
}//不是自己写的
初始值
第一个拿来开刀的就是关于初始值的问题
全局变量和static是在静态存储区的,默认初始值一般为0
而局部变量则依赖于编译器
值得一提的是,全局变量和static属于并集不空的关系
又值得一提的是,static的静态局部变量可以跨函数体使用,效果类似于全局变量
而且因为是局部变量,有助于模块化,查找起bug也容易很多
特殊函数
然后就是一个第一次见的函数,priority_queue优先队列
priority_queue<Type, Container, Functional>
如果后面两个没有的话,默认降序排列
如果第三个是greater也是
如果是less,则是升序排列
printf的使用
在一些版本,printf是不能输出String的
而取而代之的,可以在数组后加上一个:数组名.str( ).
简单理解就是进行一个转换
附加
写的好得本人都不得不说一句秀!