【问题标题】:How can i get an typedef for std::queue in c++ [duplicate]如何在 C++ 中获取 std::queue 的 typedef [重复]
【发布时间】:2020-09-13 04:05:42
【问题描述】:

我想在程序中使用 std::queue。但稍后我可能会更改为队列的其他实现。我的所有 typedef 都有一个标题,但 typedef 不适用于 std::queue。 有没有办法让 std::queue 的 typedef 有类似的东西?

编辑: 我不想要这样的特定类型的 typedef:

typedef std::queue<int> IntQueue;

我试过了

template <class T>
typedef std::queue<T> Queue;

【问题讨论】:

  • std::queue 的 typedef 是什么意思,你能举个例子
  • 您应该包含无效的内容,以便我们告诉您为什么无效。 typedef 适用于所有类型,包括 std::queue 实例。
  • 欢迎来到 Stack Overflow。请阅读the help pages,接受SO tour,阅读How to Ask,以及this question checklist。最后请edit您的问题向我们展示您尝试过的minimal reproducible example,并告诉我们它给您带来了什么问题。
  • 你尝试过使用 typedef 吗?真的只是typedef std::queue queue...?
  • 最后只是猜测您的问题:您尝试使用 模板 std::queue。问题是它不是一个类型,它是一个类型的 templatestd::queue&lt;int&gt; 是一种类型。如果要为模板本身创建别名,则必须使用 template,但必须使用 using 而不是 typedef(如 template&lt;typename T&gt; using queue_alias = std::queue&lt;T&gt;;

标签: c++ std


【解决方案1】:
template <class T>
using Queue =  std::queue<T> ;

然后在main中,你可以如下使用它

int main() {
    Queue<int> q;
    q.push(3);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2019-06-16
    • 2012-04-20
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多