954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

one key multiple value , both being strings.

Hi

I come from Python background and learning C++. In one of my programs, I need to have one key with multiple values and both being strings.

for e.g.

"str1" ---> "value of str1" 
"str2" ---> "value of str2" 
"str1" ---> "value of str1 1"
"str1" ---> "value of str1 2"


I think I need something like this:

map<string, vector<string> > mymap

My question is how do I iterate and insert?

Thanks and sorry for n00b question

imcgrath1
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

I would check the multimap container.

http://www.cplusplus.com/reference/stl/multimap/

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

How do you intend to use this?

Why wouldn't a simple std::vector be sufficient?

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

I would check the multimap container.

http://www.cplusplus.com/reference/stl/multimap/


Hi Gerard4143,

Thanks. I already implemented something like that. Here:

class outgoing
{
	multimap<string, string> outGoing;
	public:
	void add(string a, string b)
	{
		
		 outGoing.insert(pair<string, string>(a, b));
	 }
	 
	 void iter()
	 {
		 
		for (multimap<string, string>::iterator it = outGoing.begin();
       it != outGoing.end();
       ++it)
   cout << "  [" << (*it).first << ", " << (*it).second << "]" << endl;
}
 };


Code compiles fine, works fine. But when I iterate it shows like this:

str1 --> value1
str1 ---> value2
str2   -> value


How do I count number of values/attributes for a particular key? for e.g. in this case, it would be 2 for str1?

imcgrath1
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

How do you intend to use this?

Why wouldn't a simple std::vector be sufficient?

Sorry if I was not clear, it is for a graph in which a node can have multiple edges.

imcgrath1
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

So is it your intent to create data pairs, then count the number of data pairs that contain a specific first value?

EDIT: NVM, overlapped....

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

The multimap class includes a function, called equal_range , which returns the range (as iterator pair) of elements of the multimap that have the same key. Finding the number of elements with the same key is simply a matter of counting the elements in that range.

If your purpose is to construct a graph of some kind, you might want to take a look at the Boost.Graph Library (BGL) which has all sorts of graph / tree containers and algorithms.

mike_2000_17
Posting Virtuoso
Moderator
2,139 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: