944,092 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 730
  • C++ RSS
Sep 30th, 2009
-1

Multimap

Expand Post »
Hi..
Suppose I have a multimap like below
C++ Syntax (Toggle Plain Text)
  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,
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009
Oct 1st, 2009
-1

Re: Multimap

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

cpp Syntax (Toggle Plain Text)
  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
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Oct 1st, 2009
0

Re: Multimap

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:-
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009
Oct 1st, 2009
3

Re: Multimap

Hmm, I don't know why you're fixated on using a map.

Why not create your own object/class?

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 1st, 2009
0

Re: Multimap

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009
Oct 2nd, 2009
0

Re: Multimap

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 2nd, 2009
2

Re: Multimap

For example....

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 4th, 2009
0

Re: Multimap

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: may pay money for vectorized MD5
Next Thread in C++ Forum Timeline: Hidding the dos window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC