In C++11 how do I create a map of queues?

Posted on 2014-08-08
Last Modified: 2014-08-09
How do I create a map of queues and dynamically add queues to it?  Suppose I have
map<string,queue<string>> myQueues;

Open in new window

 I am doing this from a method in a class so while the following is legal, it doesn't do the job
queue<string> q1;
myQueues["foo"] = q1;

Open in new window

What I really want to do is something like this (not legal tho)
myQueues["bar"] = new queue<string>;

Open in new window

What's the legal way to do this?

Just to be clear this is a simplified and contrived example to make the question simple. In the real code, I have a producer thread managed by a class.  The map is an instance variable of the class. The class needs to sometimes dynamically create a new consumer thread and a queue to communicate with it.  The real queues need to be thread-safe queues and the map variable is actually some kind of a handle, but to keep this question simple I'm posing it with standard classes.
Question by:anAppBuilder
    LVL 30

    Accepted Solution

    Hi anAppBuilder,

    what do you mean with it doesn't do the job?

    Here I wrote a little test and IMO it works as expected:
    	using namespace std;
    	map<string,queue<string>> myQueues;
    	queue<string> q;
    	q.push( "Hello" );
    	q.push( "World" );
    	myQueues["foo"] = q;
    	myQueues["bar"].push( "Another" );
    	myQueues["bar"].push( "queue" );
    	for ( auto i = myQueues.begin(); i != myQueues.end(); i++ )
    		cout << "Key: " << i->first << endl << "Values: ";
    		while ( false == i->second.empty() )
    			cout << (i->second.front()) << ", ";
    		cout << endl;

    Open in new window

    The output is:
    Key: bar
    Values: Another, queue,
    Key: foo
    Values: Hello, World,

    Open in new window

    Please could you describe more detailed what problems you have with it?


    Author Closing Comment

    Thank you, Zoppo.   It does work.  I must have had some other problem

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now