V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
516310189i
V2EX  ›  C++

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

  •  
  •   516310189i · Jul 10, 2020 · 3430 views
    This topic created in 2119 days ago, the information mentioned may be changed or developed.

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

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

    没有发现这个问题。
    wutiantong
        5
    wutiantong  
       Jul 10, 2020   ❤️ 1
    先改成 std::queue<std::vector<int>>再聊
    Akiyu
        6
    Akiyu  
       Jul 10, 2020
    @516310189i
    http://www.cplusplus.com/reference/queue/queue/front/
    front 不会返回 void(至少不是 void 类型).
    而返回 "空" 仅在 queue 为空的情况下. 但是即使这样, 也只会在使用返回值时报错. 而并非在 front 返回的时候.
    除非编译器或者标准库做了检查. 但一般不会这样.
    方便提供一下原始代码, 库和编译器的版本么?
    516310189i
        7
    516310189i  
    OP
       Jul 10, 2020
    啊 是 CLion 的问题,CLion 提示错误,但是运行没错,是我傻了。感谢大家:)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3782 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 10:30 · PVG 18:30 · LAX 03:30 · JFK 06:30
    ♥ Do have faith in what you're doing.