Multimap

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 132
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Multimap

 
-1
  #1
Sep 30th, 2009
Hi..
Suppose I have a multimap like below
  1. multimap<string, int> m;
  2.  
  3. m.insert(pair<string, int>("a", 1));
  4. m.insert(pair<string, int>("c", 20));
  5. m.insert(pair<string, int>("b", 3));
  6. m.insert(pair<string, int>("b", 4));
  7. m.insert(pair<string, int>("a", 5));
  8. m.insert(pair<string, int>("b", 6));
I want to print a output flags for first element, middle element and last element for multi map. Like e.g,
  1. a 1 start
  2. a 5 end
  3. b 3 start
  4. b 4 middle
  5. b 6 end
  6. c 20 single

Also suppose I want to get the maximum length in a a field. Like here I should get maximum length = 2 which is for c.
Then if suppose I want to format it and set the fix length for this field as 2.

Also lets suppose I want the value to have more than one value like
  1. m.insert(pair<string, int>("a", (int1,int2,string1,string2)));
That means for my key a I have got more than 2 values. How can I make it work since pair is not working in this case ?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Multimap

 
-1
  #2
Oct 1st, 2009
Hi,
your question is bit unclear... what are you storing in the multimap logically. What I've understood is that you want to get the multiple values against the same key

  1. multimap<int, string> mm;
  2. mm.insert(std::make_pair(1, "a"));
  3. mm.insert(std::make_pair(1, "c"));
  4. mm.insert(std::make_pair(2, "d"));
  5. mm.insert(std::make_pair(2, "e"));
  6.  
  7. typedef pair<multimap<int, string>::iterator, multimap<int, string>::iterator> Pair_Range;
  8. Pair_Range pRange = mm.equal_range(1);
  9.  
  10. for (multimap<int, string>::iterator it = pRange.first; it != pRange.second; ++it) {
  11. cout << it->second<<endl;
  12. }

Hope this helps
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 132
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Re: Multimap

 
0
  #3
Oct 1st, 2009
I think I was clear in my question.

Well I solved the last part where I was showing something like this since I have no experience in using map:-
  1. m.insert(pair<string, int>("a", (int1,int2,string1,string2)));

I simply added the required string arrays like this :-

strcombo = str1 +"\t"+ str2 +"\t"+ str3

and then created map like this ;-

multimap<string , string> mmmap;

and then :-

mmmap.insert(pair<string, string>(strcombo, str1));

So it solved my problem.

Now the first problem is still there:-

1) I want to print the flags for the first / middle and the last value for each key ?

Hope I can get some advice on that.

Thanks
Last edited by Web_Sailor; Oct 1st, 2009 at 5:29 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Multimap

 
3
  #4
Oct 1st, 2009
Hmm, I don't know why you're fixated on using a map.

Why not create your own object/class?

  1.  
  2. class Something
  3. {
  4. public:
  5. char letter; //a
  6. int length; //3
  7. string pos; //start
  8. }

And then store this in a vector?
Last edited by iamthwee; Oct 1st, 2009 at 5:50 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 132
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Re: Multimap

 
0
  #5
Oct 1st, 2009
Yes I think Multimap will not be able to do such an operation. I had discussion with lot of people about it. So the best way would be to use a Vector I guess inside a beautiful class

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Multimap

 
1
  #6
Oct 2nd, 2009
Yep, and thinking out aloud...

I assume you have to add the third parameter pos which will either be single, start-end or start-middle-end and no other case.

Therefore if you sort your file by letter so that

you have:
  a 1 start
  a 5 end
  
  b 3 start
  b 4 middle
  b 6 end
  
  c 20 single

Your algo would to count the number of entries per letter.

if count = 1 then
 single
else if count = 2 then
 start end
else if count = 3 then
 start middle end
else
 discard (bad data)

And the following snippet might be useful when you come to custom sort your class...

http://www.daniweb.com/code/snippet217098.html
Last edited by iamthwee; Oct 2nd, 2009 at 5:34 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Multimap

 
2
  #7
Oct 2nd, 2009
For example....

  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Foo
  9. {
  10. public:
  11. string name;
  12. double length;
  13. };
  14.  
  15. //This functions sort by name THEN length
  16. bool SortFooByNameThenLength ( const Foo& left, const Foo& right )
  17. {
  18.  
  19. if ( left.name > right.name )
  20. {
  21. return false;
  22. }
  23. else if ( left.name < right.name )
  24. {
  25. return true;
  26. }
  27.  
  28. /*if letters are both the same now go to length and sort by
  29.   that instead
  30.   */
  31. else
  32. {
  33. /*note the difference, left.LENGTH
  34.   instead of left.NAME
  35.   */
  36. if (left.length > right.length )
  37. {
  38. return false;
  39. }
  40.  
  41. else
  42. {
  43. return true;
  44. }
  45. }
  46. }
  47.  
  48.  
  49.  
  50. int main()
  51. {
  52.  
  53. Foo test;
  54. vector <Foo> myFoo;
  55.  
  56. test.name = "a";
  57. test.length = 1;
  58.  
  59. myFoo.push_back(test);
  60.  
  61.  
  62. test.name = "b";
  63. test.length = 5;
  64.  
  65. myFoo.push_back(test);
  66.  
  67. test.name = "a";
  68. test.length = 5;
  69.  
  70. myFoo.push_back(test);
  71.  
  72. test.name = "b";
  73. test.length = 6;
  74.  
  75. myFoo.push_back(test);
  76.  
  77. test.name = "b";
  78. test.length = 3;
  79.  
  80. myFoo.push_back(test);
  81.  
  82. test.name = "c";
  83. test.length = 20;
  84.  
  85. myFoo.push_back(test);
  86.  
  87.  
  88.  
  89.  
  90. // call the sort like this
  91. std::sort(myFoo.begin(), myFoo.end(), SortFooByNameThenLength);
  92.  
  93.  
  94. for ( unsigned int i = 0; i < myFoo.size(); i++ )
  95. {
  96. cout <<myFoo[i].name << " " << myFoo[i].length << endl;
  97. }
  98.  
  99. cin.get();
  100. return 0;
  101. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 132
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Re: Multimap

 
0
  #8
Oct 4th, 2009
Great Thanks. I think it should be a good starter for me.

Appreciate your advice. I will work on it and get back if I get into some trouble.

Thanks
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC