在队列中插入向量元素

inserting vector element in queue

本文关键字:向量 元素 插入 队列      更新时间:2023-10-16
#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 << "Processt" << "Arrival Timet" << "Burst Timen";
    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]时,编译器不允许我将矢量值推到队列中:

wait.push(a[1]);

我得到以下错误

无效的参数

请查看并帮助我删除错误。

我认为您正在寻找的只是processvector,而不是processvector数组

而不是:

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] });