#include <iostream> #include <queue> using namespace std; struct Passenger { int arrival_time; // 到达时间 int service_duration; // 办理业务所需时间 }; void calculateWaitingTimes(queue<Passenger>& passengersQueue){ int currentTime = 0; long totalWaitTime = 0; while(!passengersQueue.empty()){ Passenger currentPassenger = passengersQueue.front(); passengersQueue.pop(); if(currentTime < currentPassenger.arrival_time){ currentTime = currentPassenger.arrival_time; } int waitTimeForThisPassenger = currentTime - currentPassenger.arrival_time; totalWaitTime += waitTimeForThisPassenger; cout << "Passenger waited for "<<waitTimeForThisPassenger<<" units."<< endl; currentTime += currentPassenger.service_duration; } double averageWaitTime = static_cast<double>(totalWaitTime)/static_cast<int>(!passengersQueue.empty()); cout.precision(2); cout << fixed; cout << "\nAverage waiting time:"<<averageWaitTime<<endl; } int main(){ queue<Passenger> passengerQueue; // Example data entry, replace with actual input mechanism as needed. passengerQueue.push({0, 5}); // First passenger arrives at t=0 and needs 5 unit of service passengerQueue.push({1, 2}); // Second ... passengerQueue.push({5, 3}); calculateWaitingTimes(passengerQueue); return 0; }变量名每个都改到1~4个字母
时间: 2025-06-26 11:13:07 浏览: 8
### 变量命名优化
在 C++ 中,为了提高代码的简洁性和可读性,可以通过缩短变量名称的方式实现更紧凑的表达。以下是基于提供的示例代码并将其变量名缩短至 1 到 4 个字母后的版本:
#### 修改前的原始代码
```cpp
template<typename T> auto value_of(const T v) {
if constexpr (std::is_pointer_v<T>) {
return *v; // 解引用
} else {
return v; // 返回值
}
}
int main() {
int x{47};
int* y{&x};
std::cout << format("value is {}\n", value_of(x)); // 值
std::cout << format("value is {}\n", value_of(y)); // 指针
return 0;
}
```
#### 修改后的代码
通过将较长的变量名替换为较短的形式,同时保持其意义清晰,可以得到如下代码:
```cpp
#include <iostream>
#include <type_traits>
using namespace std;
// 缩短模板函数中的变量名
template<typename T> auto val_of(const T v) {
if constexpr (is_pointer_v<T>) {
return *v; // ptr dereference
} else {
return v; // direct value
}
}
int main() {
int a{47}; // Shortened from 'x'
int* b{&a}; // Shortened from 'y'
cout << "val is " << val_of(a) << endl; // Value output
cout << "val is " << val_of(b) << endl; // Pointer dereferenced output
return 0;
}
```
上述代码中,`value_of` 函数被重命名为 `val_of`,而变量 `x` 和 `y` 分别改为 `a` 和 `b`。
---
### 结构体与队列的例子
假设存在一个涉及结构体和队列的操作场景,以下是一个完整的例子及其简化版:
#### 原始代码
```cpp
struct Student {
string name;
double gpa;
};
void process_queue(queue<Student>& q) {
while (!q.empty()) {
const Student s = q.front();
cout << "Processing student: " << s.name << ", GPA=" << s.gpa << "\n";
q.pop();
}
}
int main() {
queue<Student> students;
students.push({"Alice", 3.8});
students.push({"Bob", 3.5});
process_queue(students);
return 0;
}
```
#### 简化后的代码
```cpp
#include <queue>
#include <string>
#include <iostream>
using namespace std;
struct Std { // Shortened struct name
string nm; // Name shortened to 'nm'
double gp; // GPA shortened to 'gp'
};
void proc_q(queue<Std>& q) { // Function and variable names shortened
while (!q.empty()) {
const Std s = q.front(); // Struct instance shortened
cout << "Proc: " << s.nm << ", GP=" << s.gp << "\n"; // Output simplified
q.pop();
}
}
int main() {
queue<Std> st; // Queue of structs, renamed as 'st'
st.push({"Ali", 3.8}); // Simplified input values
st.push({"Bo", 3.5});
proc_q(st); // Call the function with shorter name
return 0;
}
```
在此过程中,`Student` 被缩写为 `Std`,字段 `name` 改为 `nm`,字段 `gpa` 改为 `gp`,方法名也进行了相应的调整。
---
### 总结
通过对变量、结构体以及函数名进行合理压缩,可以在不牺牲代码功能的前提下显著减少冗余字符的数量。这种做法尤其适用于竞赛编程或其他对性能敏感的应用场合[^1]。然而需要注意的是,在实际项目开发中应权衡代码长度与可读性的关系,避免过度追求简短而导致理解困难[^3]。
阅读全文
相关推荐

















