Hi..
Suppose I have a multimap like below

multimap<string, int> m;

  m.insert(pair<string, int>("a", 1));
  m.insert(pair<string, int>("c", 20));
  m.insert(pair<string, int>("b", 3));
  m.insert(pair<string, int>("b", 4));
  m.insert(pair<string, int>("a", 5));
  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,

a 1 start
  a 5 end
  b 3 start
  b 4 middle
  b 6 end
  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

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 :)

Recommended Answers

All 7 Replies

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

multimap<int, string>  mm;
	mm.insert(std::make_pair(1, "a"));
	mm.insert(std::make_pair(1, "c"));
	mm.insert(std::make_pair(2, "d"));
	mm.insert(std::make_pair(2, "e"));

	typedef pair<multimap<int, string>::iterator, multimap<int, string>::iterator>  Pair_Range;
	Pair_Range  pRange = mm.equal_range(1);
	
	for (multimap<int, string>::iterator it = pRange.first; it != pRange.second; ++it) {
		cout << it->second<<endl;
	}

Hope this helps

commented: no -4

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:-

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 :)

Member Avatar for iamthwee

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

Why not create your own object/class?

class Something
{
public:
     char letter;  //a
     int length;   //3
     string pos;  //start
}

And then store this in a vector?

commented: I agree. map is not the right container. +36
commented: Good suggestions through the whole thread. +24

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

Member Avatar for iamthwee

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

Member Avatar for iamthwee

For example....

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

class Foo
{
public:
   string name;
   double length;
};

//This functions sort by name THEN length
bool SortFooByNameThenLength ( const Foo& left, const Foo& right )
{

   if ( left.name > right.name )
   {
      return false;
   }
   else if ( left.name < right.name )
   {
      return true;
   }
   
   /*if letters are both the same now go to length and sort by
     that instead
     */
   else 
   {  
      /*note the difference, left.LENGTH
        instead of left.NAME
        */
      if (left.length > right.length )
      {
        return false;
      }
      
      else 
      { 
        return true;
      }  
   }
}



int main()
{
    
   Foo test;
   vector <Foo> myFoo;

   test.name = "a";
   test.length = 1;

   myFoo.push_back(test);
   
  
   test.name = "b";
   test.length = 5;
   
   myFoo.push_back(test);

   test.name = "a";
   test.length = 5;
   
   myFoo.push_back(test);
   
   test.name = "b";
   test.length = 6;
   
   myFoo.push_back(test);
   
   test.name = "b";
   test.length = 3;
   
   myFoo.push_back(test);
   
   test.name = "c";
   test.length = 20;
   
   myFoo.push_back(test);
   



   // call the sort like this 
   std::sort(myFoo.begin(), myFoo.end(), SortFooByNameThenLength);
   
   
   for ( unsigned int i = 0; i < myFoo.size(); i++ )
   {
     cout <<myFoo[i].name << " " << myFoo[i].length << endl;
   }
   
   cin.get();
   return 0;
}
commented: Great! +18

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.