gpt4 book ai didi

c++ - 在队列中插入 vector 元素

转载 作者:行者123 更新时间:2023-11-28 06:16:34 24 4
gpt4 key购买 nike

#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int main()
{
struct process
{
int burst;
int ar;
};
int x = 4;
int arival[x];
int burst[x];
ifstream arrival, burrst;
arrival.open("arrival.txt");
burrst.open("burrst.txt");
for (int i = 0; i<x; i++)
{
arrival >> arival[i];
burrst >> burst[i];

}
arrival.close();
burrst.close();


vector<process> a[x];

for (int i = 0; i<x; i++)
{
a[i].push_back({ burst[i], arival[i] });

}
cout << "Process\t" << "Arrival Time\t" << "Burst Time\n";
for (int i = 0; i<x; i++)
{
char k = 'A';
cout << k << "\t" << a[i].back().burst << "\t" << a[i].back().ar << endl;
}
queue<process> wait, ready; /* Declare a queue */
wait.push(a[1]);
return 0;
}

当我尝试将 a[1] 插入等待队列时,编译器不允许我将 vector 值压入队列:

wait.push(a[1]);

出现以下错误

invalid arguments

请看一下并帮助我消除错误。

最佳答案

我认为您正在寻找的只是一个 vectorprocess ,不是 vector 的数组的 process

代替:

vector<process> a[x];

使用:

vector<process> a;

然后,在 for 循环中,使用:

// a[i].push_back({ burst[i], arival[i] });
// ^^^ drop this.

a.push_back({ burst[i], arival[i] });

关于c++ - 在队列中插入 vector 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30171120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com