V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
516310189i
V2EX  ›  C++

C++ queue 中不可以存放 vector<int>吗?

  •  
  •   516310189i · 2020-07-10 09:58:05 +08:00 · 2635 次点击
    这是一个创建于 1357 天前的主题,其中的信息可能已经有所发展或是发生改变。

    queue<vector<int>> q; 当调用 q.front()时为什么返回值是 void 呢

    7 条回复    2020-07-10 12:47:10 +08:00
    dbskcnc
        1
    dbskcnc  
       2020-07-10 10:00:05 +08:00
    完全没问题
    BrettD
        2
    BrettD  
       2020-07-10 10:07:02 +08:00 via iPhone
    你怎么看到返回 void 的?
    516310189i
        3
    516310189i  
    OP
       2020-07-10 10:37:14 +08:00
    使用 auto a = q.front();的时候会提示错误
    Cant instantiate the variables of type void
    CLion 里提示也显示 front 返回值是 void
    coderfox
        4
    coderfox  
       2020-07-10 11:19:01 +08:00
    http://cpp.sh/6cs3h

    没有发现这个问题。
    wutiantong
        5
    wutiantong  
       2020-07-10 11:20:49 +08:00   ❤️ 1
    先改成 std::queue<std::vector<int>>再聊
    Akiyu
        6
    Akiyu  
       2020-07-10 11:52:10 +08:00
    @516310189i
    http://www.cplusplus.com/reference/queue/queue/front/
    front 不会返回 void(至少不是 void 类型).
    而返回 "空" 仅在 queue 为空的情况下. 但是即使这样, 也只会在使用返回值时报错. 而并非在 front 返回的时候.
    除非编译器或者标准库做了检查. 但一般不会这样.
    方便提供一下原始代码, 库和编译器的版本么?
    516310189i
        7
    516310189i  
    OP
       2020-07-10 12:47:10 +08:00
    啊 是 CLion 的问题,CLion 提示错误,但是运行没错,是我傻了。感谢大家:)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4101 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:17 · PVG 18:17 · LAX 03:17 · JFK 06:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.