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()); ???
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));
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 ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With