在队列中插入矢量元素

时间:2015-05-11 15:01:09

标签: c++ vector struct queue

#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]时:

wait.push(a[1]);

我收到以下错误

  

无效参数

请查看并帮我删除错误。

1 个答案:

答案 0 :(得分:1)

我认为您所寻找的只是vector process,而不是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] });