I am able to build a logic which does exactly how I want it to work but when I add more records there are some problems in flagging.

Here is the example:-
when my input is :

mymm.insert(pair<char,int>('a',50));
  mymm.insert(pair<char,int>('b',100));
  mymm.insert(pair<char,int>('b',150));
  mymm.insert(pair<char,int>('b',200));
  mymm.insert(pair<char,int>('c',250));
  mymm.insert(pair<char,int>('c',300));
  mymm.insert(pair<char,int>('d',300));

output is correct :-

single	50
starterminal	100
middle	150
terminal	200
starterminal	250
terminal	300
single	300

But when I change my input like this:-

mymm.insert(pair<string,string>('a',50));
  mymm.insert(pair<char,int>('b',100));
  mymm.insert(pair<char,int>('b',150));
  mymm.insert(pair<char,int>('b',200));
  mymm.insert(pair<char,int>('c',250));
  mymm.insert(pair<char,int>('c',300));
  mymm.insert(pair<char,int>('d',300));
  mymm.insert(pair<char,int>('e',250));
  mymm.insert(pair<char,int>('e',300));

The output is not correct:-

single	50
starterminal	100
middle	150
terminal	200
starterminal	250
terminal	300
single	300
terminal   250
terninal    300

Here is my code:-

// multimap::count
#include <iostream>
#include <map>
#include<vector>
using namespace std;

int main ()
{
  multimap<char,int> mymm;
  multimap<char,int>::iterator it;
  multimap<char, int>::const_iterator m1_cIter;
  char c;

  mymm.insert(pair<string,string>('a',50));
  mymm.insert(pair<char,int>('b',100));
  mymm.insert(pair<char,int>('b',150));
  mymm.insert(pair<char,int>('b',200));
  mymm.insert(pair<char,int>('c',250));
  mymm.insert(pair<char,int>('c',300));
  mymm.insert(pair<char,int>('d',300));
  mymm.insert(pair<char,int>('e',250));
  mymm.insert(pair<char,int>('e',300));


  int i = 0;
  int k = 0;
  for (c='a'; c<='z'; c++)
  {
    for (it=mymm.equal_range(c).first; it!=mymm.equal_range(c).second; ++it)
      if((int)mymm.count(c)>2){      
         if(i==0){
            cout << "start"<<"\t"<<(*it).second << endl;
         }    
         else if(i>0 && i < ( mymm.count(c) - 1)){
            cout << "middle"<<"\t"<<(*it).second << endl;
         }
         else {     
            cout << "terminal"<<"\t"<<(*it).second << endl;
         }
       i++;
      }
/////////////
      else if((int)mymm.count(c)==1){      
            cout << "single"<<"\t"<<(*it).second << endl;   
      }
/////////////
/////////////
      else if((int)mymm.count(c)==2){      
         if(k==0){
            cout << "start" << "\t"<< (*it).second << endl;
         }
         else {
            cout << "terminal" << "\t" << (*it).second << endl; 
         } 
         k++;
      }
////////////
  }
  return 0;
}

Can somebody point out my mistake ? Where am I doing mistake ?

Thanks

Recommended Answers

All 17 Replies

u are not resetting the variables i and k to zero and due to which the condition

if(i==0)

and

if(k==0)

fails after u have completely printed one character range.

How can I reset it.. any example ?

Thanks

How can I reset it.. any example ?

Thanks

do it just at the beginning of your loop as follows:

for (c='a'; c<='z'; c++)  
{
      i=0;
      k=0;
/**remaining code/
}

Sorry any example ?

Sorry any example ?

see my prev post

Ok ok .. Thanks... I will try it :)
Any other suggestions ?
Now if I want to change my multimap from <char, int>
to <string, string > any suggestions ?

Thanks

Ok ok .. Thanks... I will try it :)
Any other suggestions ?
Now if I want to change my multimap from <char, int>
to <string, string > any suggestions ?

Thanks

yes you can do it easily. Post here if you get any issues.

One more suggestion is read about whatever u want to use. Use google. There are lot of materials available on net. :)
enjoy

Hi. I changed my program to something like this but I am getting duplicate records in my result. Where could be the problem ?

// multimap::count
#include <iostream>
#include <map>
#include<vector>
using namespace std;

int main ()
{
  multimap<char,int> mymm;
  multimap<char,int>::iterator it;
  multimap<char, int>::const_iterator m1_cIter;
  vector<char> vec1;
  vector<char>::iterator itv;
  char c;

  mymm.insert(pair<char,int>('a',50));
  mymm.insert(pair<char,int>('b',100));
  mymm.insert(pair<char,int>('b',150));
  mymm.insert(pair<char,int>('b',200));
  mymm.insert(pair<char,int>('c',250));
  mymm.insert(pair<char,int>('c',300));
  mymm.insert(pair<char,int>('d',300));
  mymm.insert(pair<char,int>('e',250));
  mymm.insert(pair<char,int>('e',300));
  mymm.insert(pair<char,int>('e',400));
  mymm.insert(pair<char,int>('e',500));
  mymm.insert(pair<char,int>('e',600));
  mymm.insert(pair<char,int>('n',800));
  mymm.insert(pair<char,int>('k',950));
  mymm.insert(pair<char,int>('k',1000));

  vec1.push_back('a');
    vec1.push_back('b');
     vec1.push_back('b');
        vec1.push_back('b');
          vec1.push_back('c');
            vec1.push_back('c');
              vec1.push_back('d');
                vec1.push_back('e');
                  vec1.push_back('e');
                    vec1.push_back('e');
                      vec1.push_back('e');  
                      vec1.push_back('e');
                        vec1.push_back('n');
                          vec1.push_back('k');
                            vec1.push_back('k');
                             
   
 
  //for (c='a'; c<='z'; c++)
    for(itv=vec1.begin(); itv < vec1.end(); itv++)
  {
       int i = 0;
       int k = 0;
    for (it=mymm.equal_range(*itv).first; it!=mymm.equal_range(*itv).second; ++it)
      if((int)mymm.count(*itv)>2){      
         if(i==0){
            cout << "S"<<"\t"<<(*it).second << endl;
         }    
         else if(i>0 && i < ( mymm.count(*itv) - 1)){
            cout << "M"<<"\t"<<(*it).second << endl;
         }
         else if(i !=0 ){     
            cout << "T"<<"\t"<<(*it).second << endl;
         }
       i++;
      }
/////////////
      else if((int)mymm.count(*itv)==1){      
            cout << "S*"<<"\t"<<(*it).second << endl;   
      }
/////////////
/////////////
      else if((int)mymm.count(*itv)==2){      
         if(k==0){
            cout << "S" << "\t"<< (*it).second << endl;
         }
         else {
            cout << "T" << "\t" << (*it).second << endl; 
         } 
         k++;
      }
////////////
  }
  return 0;
}

Thanks

ofcource u will get duplicates because u are doing

for(itv=vec1.begin(); itv < vec1.end(); itv++){...}

and this vector contain all those character more than once.
U must skip those character which are already printed.

Dont do the following

vec1.push_back('a');    
vec1.push_back('b');     
vec1.push_back('b');        
vec1.push_back('b');          
vec1.push_back('c');            
vec1.push_back('c');              
vec1.push_back('d');                
vec1.push_back('e');                  
vec1.push_back('e');                    
vec1.push_back('e');                      
vec1.push_back('e');                        
vec1.push_back('e');                        
vec1.push_back('n');                          
vec1.push_back('k');                            
vec1.push_back('k');

push a character only once

vec1.push_back('a');    
vec1.push_back('b');     
vec1.push_back('c');              
vec1.push_back('d');                
vec1.push_back('e');                  
vec1.push_back('n');                          
vec1.push_back('k');

Ya I was not removing the duplicates.

Thanks for your help :)

Ya I was not removing the duplicates.

Thanks for your help :)

u welcome :)

One last question before I mark the thread solved :)

If I have a data set lie this:-

mymm.insert(pair<char,int>("andrew","50 years old"));
  mymm.insert(pair<char,int>("peter","50 years old"));
  mymm.insert(pair<char,int>("peter","50 years old"));
  mymm.insert(pair<char,int>("peter","50 years old"));
  mymm.insert(pair<char,int>("mathwew","50 years old"));
  mymm.insert(pair<char,int>("mathew","50 years old"));
  mymm.insert(pair<char,int>("D"souza","50 years old"));
  mymm.insert(pair<char,int>("Austin","50 years old"));
  mymm.insert(pair<char,int>("Austin","50 years old"));
  mymm.insert(pair<char,int>("Austin","50 years old"));
  mymm.insert(pair<char,int>("Austin","50 years old"));
  mymm.insert(pair<char,int>("Austin","50 years old"));
  mymm.insert(pair<char,int>("David","50 years old"));
  mymm.insert(pair<char,int>("John","50 years old"));
  mymm.insert(pair<char,int>("John","50 years old"));

Thanks

it cannot be like

mymm.insert(pair<char,int>("andrew","50 years old"));

it had to be

mymm.insert(pair<string, string>("andrew","50 years old"));

because the values u are passing are strings.

Will
multimap<string, string>
behave as this one is doing.

Thanks a lot

Will
multimap<string, string>
behave as this one is doing.

Thanks a lot

yep it should.

Thanks a lot for all your help :)
It was really helpful :)

I have marked the thread as solved :)

Thanks :)

Thanks a lot for all your help :)
It was really helpful :)

I have marked the thread as solved :)

Thanks :)

my pleasure :)

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.