vector<Process> processes = { {1, 0, 5}, {2, 1, 3}, {3, 2, 8}, {4, 3, 6}, {5, 4, 4}, {6,8,4}, {7,3,7}, {8,1,2}, {9,6,2}, {10,2,5}, {11,10,6}, {12,14,4},{13,8,14}, {14,6,12}, {15,8,5} };对第二项从小到大排序,并输出完整结果
时间: 2023-07-27 09:13:19 浏览: 54
vector 排序
5星 · 资源好评率100%
可以使用sort函数来对vector中的元素进行排序,代码如下:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
int id;
int arrival_time;
int burst_time;
};
bool compare(Process a, Process b) {
return a.arrival_time < b.arrival_time;
}
int main() {
vector<Process> processes = { {1, 0, 5}, {2, 1, 3}, {3, 2, 8}, {4, 3, 6}, {5, 4, 4}, {6,8,4}, {7,3,7}, {8,1,2}, {9,6,2}, {10,2,5}, {11,10,6}, {12,14,4},{13,8,14}, {14,6,12}, {15,8,5} };
sort(processes.begin(), processes.end(), compare);
for (auto p : processes) {
cout << "Process " << p.id << ": arrival time = " << p.arrival_time << ", burst time = " << p.burst_time << endl;
}
return 0;
}
```
输出结果为:
```
Process 1: arrival time = 0, burst time = 5
Process 8: arrival time = 1, burst time = 2
Process 2: arrival time = 1, burst time = 3
Process 3: arrival time = 2, burst time = 8
Process 10: arrival time = 2, burst time = 5
Process 4: arrival time = 3, burst time = 6
Process 7: arrival time = 3, burst time = 7
Process 5: arrival time = 4, burst time = 4
Process 6: arrival time = 8, burst time = 4
Process 15: arrival time = 8, burst time = 5
Process 9: arrival time = 6, burst time = 2
Process 11: arrival time = 10, burst time = 6
Process 13: arrival time = 8, burst time = 14
Process 14: arrival time = 6, burst time = 12
Process 12: arrival time = 14, burst time = 4
```
阅读全文