How can I copy an entire vector into a queue?

前端 未结 3 965
滥情空心
滥情空心 2021-02-14 08:09

I am looking to copy the entire contents of a vector into a queue in C++. Is this a built in function or is it nessesary to loop over each element?

3条回答
  •  春和景丽
    2021-02-14 08:51

    The queue's constructor is as follows:

    explicit queue ( const Container& ctnr = Container() );
    

    So you can have some vector v and construct a queue from it.

    vector v;
    deque d;
    /* some random magic code goes here */
    queue> q(d(v));
    

    However you can't do this to push_back elements in an already initialized q. You could use another Container, empty your queue, append your vector to that container, and create a new queue from that vector; but I'd iterate rather than doing all that.

    Final answer: No, there is no such method implemented for queues, you could use deque or iterate your vector.

提交回复
热议问题