在队列中推动结构变量

pushing structure variable in queue

本文关键字:结构 变量 队列      更新时间:2023-10-16
#include <iostream>
#include <queue>
using namespace std;
int main () {
    struct process {
        int burst;
        int ar;
    };
    int x=4;
    process a[x];
    queue <string> names; /* Declare a queue */
    names.push(a[1]);
    return 0;
}

我正在尝试在队列中推动结构变量,但它不采取并给出错误

no matching function for #include queue and invalid argument

我该怎么做?

c 是一种强烈打字的语言。在names.push(a[1]);行中,您正在尝试将struct(从process a[x];数组)推入queue<string>。您的结构不是string,因此编译器会发出错误。您至少需要queue<process>

其他问题:可变长度阵列不是标准的C (process a[x];)。改用std::vector<process>。这是一些有效的简单示例:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main () {
    struct process // move this outside of main() if you don't compile with C++11 support
    {
        int burst;
        int ar;
    };
    vector<process> a;
    // insert two processes
    a.push_back({21, 42});
    a.push_back({10, 20});
    queue <process> names; /* Declare a queue */
    names.push(a[1]); // now we can push the second element, same type
    return 0; // no need for this, really
}

编辑

用于实例化模板的局部定义的类/结构仅在C 11和更高版本中有效,请参见例如为什么我可以在C 中的功能中定义结构和类?和内部的答案。如果您无法访问C 11符合编译器,请在main()之外移动struct定义。