【问题标题】:How do I get values from 2d QUEUE [closed]如何从 2d QUEUE [关闭]
【发布时间】:2020-11-23 05:33:24
【问题描述】:
#include <iostream>
#include <queue>
using namespace std;

int main(){
    queue<int, int> q1;
    q1.push({3, 5});
    int x = q1.front().first;
    int y = q2.front().second;
    cout << x, y << endl;

    return 0;
}

不知道是哪里出错了,请指点一下好吗?

【问题讨论】:

  • 显示您的代码。不知道queue 是什么。 minimal reproducible example
  • 你在哪里定义了 q2?为什么你使用 q2.front(),而不是 q1?

标签: c++ queue


【解决方案1】:

如果您的目标是将std::pair&lt;int, int&gt; 存储在std::queue 中,则执行以下操作:

#include <iostream>
#include <queue>
#include <utility>

int main()
{
    std::queue<std::pair<int, int>> q1;  // <-- Note the declaration here
    q1.push({3, 5});
    int x = q1.front().first;
    int y = q1.front().second;
    std::cout << x << " "  << y;
    return 0;
}

输出:

3 5

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多