Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a queue with n default values?

Tags:

c++

c++11

stl

queue

Say I have a class with default constructor. How can I initialize a queue in constructor by settings its size and a default value.

class StandardClass
{};

// will initialize a vector with 5 default standard class 
std::vector<StandardClass> vec(5, StandardClass()); 

How do I do the same with queue?

std::queue<StandardClass> que(5, StandardClass()); ???
like image 598
user2822838 Avatar asked Mar 16 '15 19:03

user2822838


2 Answers

If you look at e.g. this std::queue constructor reference you will see that you can pass an instance of the underlying container (defaults to std::deque).

So you should be able do something like

std::queue<StandardClass> que(std::deque<StandardClass>(5));
like image 51
Some programmer dude Avatar answered Nov 14 '22 23:11

Some programmer dude


You can do it the following way

std::queue<StandardClass> que
    ( std::queue<StandardClass>::container_type( 5, StandardClass()  ) );

Or more simpler

std::queue<StandardClass> que
    ( std::queue<StandardClass>::container_type( 5 ) );
like image 44
Vlad from Moscow Avatar answered Nov 14 '22 23:11

Vlad from Moscow