Which container - std::pair or std::map ?

B

Bart Simpson

I am wriing a messaging system and I need to mantain a list of
subscribers(object) to topics (string).

which of the ff representations is better (and why?)

typedef pair<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;

OR

typedef map<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;
 
J

Jakob Bieling

Bart Simpson said:
which of the ff representations is better (and why?)

typedef pair<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;

OR

typedef map<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;

Are you aware of the fact that a map and a pair are pretty much
different? A pair will let you treat two objects as one -- a pair! A map
is one way to manage *several* pairs.

hth
 
B

Ben Pope

Bart said:
I am wriing a messaging system and I need to mantain a list of
subscribers(object) to topics (string).

which of the ff representations is better (and why?)

typedef pair<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;

OR

typedef map<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;

What about:
typedef std::multimap<std::string, Subscriber> subscriberList;

I'm not sure this is entirely appropriate for the task, anyway (although
I can't tell because your description of the task is not complete enough).

Have you looked at the various existing signals & slots libraries?

Ben Pope
 
D

Daniel T.

Bart Simpson said:
I am wriing a messaging system and I need to mantain a list of
subscribers(object) to topics (string).

which of the ff representations is better (and why?)

typedef pair<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;

OR

typedef map<string, list<Subscriber> > topicSubscribers ;
typedef list<topicSubscribers> subscriberList ;

Better for what? The former allows you to maintain the topicSubscribers
in any order you want and allows multiple topics to have the same name,
whereas the latter makes is faster to find a particular subscriberList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top